- This topic has 9 replies, 3 voices, and was last updated 3 years, 12 months ago by David.
-
AuthorPosts
-
April 19, 2020 at 9:52 pm #1246073Tim
Hello
Previously I have simply used CC Child Pages plugin shortcode to list Title, Featured Image etc.
Now I would like to list only an ACF “logo” image field that I have for each “client” Child of an array of Parent Pages. These I intend to display in the Client and Partner sections of the home page.
I have displayed ACF fields in WP Show Posts in Element custom hooks like
wpsp_after_title
, but I’m not sure if I can do the same with CC Child Pages, at least I don’t find such hooks documented.Otherwise, is there a less convoluted way to get and display this list of images ?
April 20, 2020 at 2:14 am #1246255DavidStaffCustomer SupportHi there,
for the CC Child Pages you would need to ask the plugin author support about their hooks.
But you would probably be better off asking ACF support or searching there forums and skipping CC Child Pages for that need
April 21, 2020 at 12:32 am #1247520TimYes, ACF probably will be the better way to go.
I suppose I’m asking how I can display a list of an image field eg “logo” that belong to Child Pages of a Page Parent array.
And if I can do that, I would be able to replace all use of CC Child Pages plugin.
April 21, 2020 at 6:38 am #1247813DavidStaffCustomer SupportBest to ask ACF for the best method.
This topic may get you started:April 21, 2020 at 7:09 pm #1248662TimHi David
Thank you for the pointer.
Whilst the topic is very close to mine, the additional complexity of the OP’s additional requirements is overwhelming my comprehension of the code. I think I need a simpler example.
I did find another example here:
https://support.advancedcustomfields.com/forums/topic/list-items-and-their-acfields-from-child-pages-on-parent-page/<?php $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) ); foreach( $mypages as $page ) { $content = $page->post_content; if ( ! $content ) // Check for empty page continue; $content = apply_filters( 'the_content', $content ); ?> <?php echo $page->post_title; ?><br> <?php } ?>
At this point I’m just trying to get a list of the children of the page that the code is run on, using an Element hooked “after_main_content”, with Display Location on one of my parent pages, but I’m getting what appears to be a list of top level and 2nd level pages. I also get the errors:
Notice: Undefined variable: post in /home/resolv6/dev.resolve.mg/wp-content/plugins/gp-premium/elements/class-hooks.php(180) : eval()’d code on line 2
Notice: Trying to get property ‘ID’ of non-object in /home/resolv6/dev.resolve.mg/wp-content/plugins/gp-premium/elements/class-hooks.php(180) : eval()’d code on line 2
I also found another example here:
https://support.advancedcustomfields.com/forums/topic/show-child-fields-on-parent-page/The furthest I managed to get with that was:
<?php $pages = array('child_of' => $post->ID, 'parent' => $post->ID ); $packages = get_pages($pages); foreach ($packages as $package){ echo '=========' . $package->post_title . '=============='; ?> <?php the_field('title', $package->ID); ?> <?php } ?>
But it seems that
<?php the_field('title', $package->ID); ?>
doesn’t display anything and'child_of' => $post->ID, 'parent' => $post->ID
only displays top level pages and'child_of' => $post->ID
alone displays all pages.I also get these errors:
Notice: Undefined variable: post in /home/resolv6/dev.resolve.mg/wp-content/plugins/gp-premium/elements/class-hooks.php(180) : eval()’d code on line 1
Notice: Trying to get property ‘ID’ of non-object in /home/resolv6/dev.resolve.mg/wp-content/plugins/gp-premium/elements/class-hooks.php(180) : eval()’d code on line 1
I also found this example:
<?php $args = array( 'post_type' => 'page', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order' ); $mypages = new WP_Query( $args ); if ( $mypages->have_posts() ) : ?> <?php while ( $mypages->have_posts() ) : $mypages->the_post(); ?> <p style="color: white; text-transform: uppercase;"><?php the_title(); ?></p> <?php endwhile; ?> <?php endif; wp_reset_query(); ?>
And got those same errors for line 6, which leads me to believe there is something amiss with
$post->ID
So do I have to define this somewhere ? I thought $post was already part of wp.April 22, 2020 at 9:16 am #1249489TomLead DeveloperLead DeveloperYou need to set the
$post
variable in your code. For example:<?php global $post; $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) ); foreach( $mypages as $page ) { $content = $page->post_content; if ( ! $content ) // Check for empty page continue; $content = apply_filters( 'the_content', $content ); ?> <?php echo $page->post_title; ?><br> <?php } ?>
April 22, 2020 at 6:57 pm #1250209TimHi Tom
Thank you for that. No more errors.
However, I did need to remove
if ( ! $content ) continue;
for the content to be displayed.
I can now display the intended content from children of 1 parent.
However, my intention is to display content from children of multiple parents.
It seems that
child_of
expects an integer, so whilst'child_of' => 1291,
is acceptable'child_of' => (1291, 1326),
is not.I then found this unaccepted answer: https://stackoverflow.com/a/32535618
Which provides an alternative method but still using
child_of
.I have now managed to display the intended ACF content from children of multiple parent IDs.
<div class="grid"> <?php global $post; $mypages = array(); foreach (array(1291,1326,1275,1301,1261,1247,1322) as $pages) { $mypages = array_merge($mypages, get_pages(array('child_of' => $pages, 'sort_column' => 'post_date', 'sort_order' => 'desc' ))); }; foreach( $mypages as $pages ) { $content = $pages->post_content; $content = apply_filters( 'the_content', $content ); ?> <div class="entry-content" itemprop="text"> <div class="client-logo-header"> <figure class="client-logo-media"> <?php $logo = get_field('logo', $pages->ID); $size = 'medium'; // (thumbnail, medium, large, full or custom size) $client_link = esc_url( get_permalink($pages->ID) ); if( $logo ) { echo '<a href="' . $client_link .'">' . wp_get_attachment_image( $logo, $size ) . '</a>'; } ?> </figure> </div> </div> <?php } ?> </div>
However, the child pages that do not have the required
logo
render empty elements. How can I filter out those with the missing content ?April 23, 2020 at 6:11 am #1251162DavidStaffCustomer SupportTry:
<div class="grid"> <?php global $post; $mypages = array(); foreach (array(1291,1326,1275,1301,1261,1247,1322) as $pages) { $mypages = array_merge($mypages, get_pages(array('child_of' => $pages, 'sort_column' => 'post_date', 'sort_order' => 'desc' ))); }; foreach( $mypages as $pages ) { $content = $pages->post_content; $content = apply_filters( 'the_content', $content ); // Get logo fields $logo = get_field('logo', $pages->ID); $size = 'medium'; // (thumbnail, medium, large, full or custom size) $client_link = esc_url( get_permalink($pages->ID) ); // If Logo then Display it if( $logo ) { ?> <div class="entry-content" itemprop="text"> <div class="client-logo-header"> <figure class="client-logo-media"> <?php echo '<a href="' . $client_link .'">' . wp_get_attachment_image( $logo, $size ) . '</a>';?> </figure> </div> </div> <?php } ?> </div>
April 23, 2020 at 6:41 pm #1252423TimThank you David and for adding the comments 🙂
It took me a while to figure out where to put the missing brace:
<?php } } ?>
April 24, 2020 at 1:46 am #1252811DavidStaffCustomer Supportoops – glad you got it working
-
AuthorPosts
- You must be logged in to reply to this topic.