Support » Fixing WordPress » Show list of Child pages of current page w/ Featured Image

  • Resolved brittgamil

    (@brittgamil)


    I would like to show a list of the child pages of the current page with their prospective “Featured Images” INSIDE THE LOOP.

    I am able to produce the list of child pages, but haven’t been able to get the Featured Image to be pulled in.

    This is what I have inside the loop to echo the child page names/links

    <?php
      $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
      if ($children) { ?>
      <ul>
    <?php echo   $children; ?>
    
      </ul>
      <?php } ?>

    Any help to get me on the right path of adding the images would be great 🙂

    THANKS ahead of time!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter brittgamil

    (@brittgamil)

    Okay I’ve figured it out… Hopefully this will help someone else. I used this code and edited it for the Get_the_post_thumbnail

    This lists my child pages with their feature image thumbnail INSIDE THE LOOP of the page

    <?php
    $child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = ".$post->ID."    AND post_type = 'page' ORDER BY menu_order", 'OBJECT');    ?>
    <?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
    <div class="child-thumb">
      <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
     <a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a>
    </div>
    <?php endforeach; endif;
    ?>

    Thanks Brittgamil.

    I used your code (with some tiny adjustments) to display a rotating image on a WordPress CMS home page so that clients can add a page with a featured image mark the parent as another page then select all the child pages of this page with featured image and display with JS. Works a treat.

    Did you know that in WP 3.0 you can use the ‘the_post_thumbnail()’ and you can also set the sizes so can have your own sizes i.e. the_post_thumbnail(‘my-special-thumbnail-size’). Sorry if you already knew, just wanted to add something.

    Vinny

    Thanks for the code.

    I modified it to get the thumbnail of each Sub-Page instead of the the thumbnail of the current page: $page->ID replaced with $page->pageChild.

    I also got the link to encapsulate both image and text to create a menu.

    <?php
                            $child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = ".$post->ID."    AND post_type = 'page' ORDER BY menu_order", 'OBJECT');    ?>
                            <?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
    
                            <div class="child-thumb">
                             <a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>">
                                <?php echo get_the_post_thumbnail($pageChild->ID, 'single-post-thumbnail'); ?>
                                <?php echo $pageChild->post_title; ?>
                             </a>
                            </div>

    Thank you a million times over.
    Sweatin bullets due to some freak last minute thing where my thumbs stopped working the day the client’s showing the boss.

    Saved my hyde. Cheers!

    Thanks Britt, that was extremely helpful.

    However I noticed one small change was needed to ensure that the featured image that was pulled was for the actual child page and not the current page.

    <?php
    $child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = ".$post->ID."    AND post_type = 'page' ORDER BY menu_order", 'OBJECT');    ?>
    <?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
    <div class="child-thumb">
      <?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?>
     <a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a>
    </div>
    <?php endforeach; endif;
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Show list of Child pages of current page w/ Featured Image’ is closed to new replies.