Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have tried to achieve that the Home page displays featured images of all it's child pages and in turn some child pages are displaying the featured images of their child pages. This is my code so far!

<?php
        global $post;

        if ( is_page() && $post->post_parent ) : 

        elseif ( is_page() && $post->post_parent && $post->child_page ) : ?>

        <?php else : ?>
            <?
            $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');

            if ( $child_pages ) :
                foreach ( $child_pages as $pageChild ) :
                    setup_postdata( $pageChild );
                    $thumbnail = get_the_post_thumbnail($pageChild->ID, 'index-thumb');
                    if($thumbnail == "") continue; // Skip pages without a thumbnail
            ?>
                    <div class="child-thumb">
                    <?php
                        echo '<div class="re-featured-image">' . get_the_post_thumbnail($pageChild->ID, 'index-thumb', array('class' => 'img-responsive', 'id' => 'img-center')) . '</div>';
                        echo '<div class="re-featured-image-title">' . get_post(get_post_thumbnail_id($pageChild->ID))->post_title . '</div>';
                        echo '<div class="re-featured-image-caption">' . get_post(get_post_thumbnail_id($pageChild->ID))->post_excerpt . '</div>';
                        echo '<div class="re-featured-image-description">' . get_post(get_post_thumbnail_id($pageChild->ID))->post_content . '</div>';
                    ?>                      
                    </div>
            <?
                endforeach;
            endif;
            ?>
        <?php endif; ?>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

I think you can use a different approach: write a custom sql query that get the ids of the thumbnails attachment of the children pages, and if found call wp_get_attachment_image_src on this ids to retrieve the urls:

function my_get_thumbnails( $post = NULL, $which = 'both' ) {
  // first we get the post, if no post is passed we use global post
  if ( empty( $post ) ) global $post;
  if ( is_numeric($post) ) $post = get_post( $post );
  if ( ! $post instanceof WP_Post ) return;
  $children = FALSE;
  $parent = FALSE;
  // if we want children posts thumbnail...
  if ( $which !== 'parent' ) {
    // run a query to get attachment id set as thumbnail in children posts
    global $wpdb;
    $q = "SELECT {$wpdb->postmeta}.meta_value FROM {$wpdb->postmeta}
    JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
    WHERE {$wpdb->postmeta}.meta_key = '_thumbnail_id'
    AND {$wpdb->posts}.post_parent = %d";
    $thumb_ids = $wpdb->get_col( $wpdb->prepare( $q, $post->ID ) );
    // if some results, call wp_get_attachment_image_src on all ids to get image urls
    if ( ! empty( $thumb_ids ) ) {
      $children = array_map( function($tid) {
         $img = wp_get_attachment_image_src( $tid, 'large' ); // <-- SET SIZE HERE
         return $img[0];
      }, $thumb_ids );
    }
  }
  // if we want parent post thumbnail...
  if ( $which !== 'children' && $post->post_parent ) {
    $tid = get_post_thumbnail_id( $post->post_parent );
    if ( $tid ) {
      // get the url of parent post thumbnail
      $img = wp_get_attachment_image_src( $tid, 'large' );  // <-- SET SIZE HERE
      $parent = $img[0];
    }
  }
  // if we want only children posts thumbnail return them
  if ( $which === 'children' ) return $children;
  // if we want only parent post thumbnail return it
  if ( $which === 'parent' ) return $parent;
  // if we want bot return an array with both
  return array( 'children' => $children, 'parent' => $parent );
}

After you have entered previous function in functions.php, in your template (probably page.php), use it like so:

<?php
global $post;
if ( is_page() ) {

  $thumbnails = my_get_thumbnails();

  if ( ! empty( $thumbnails['parent'] ) ) {
    $format = '<div class="parent-thumb"><img src="%s" alt="" /></div>';
    printf( $format, $thumbnails['parent'] );
  }

  if ( is_array( $thumbnails['children'] ) && ! empty( $thumbnails['children'] ) ) {
    $open = '<div class="child-thumb"><img src="';
    $close = '" alt="" /></div>';
    echo $open . implode( $close . $open, $thumbnails['children'] ) . $close;
  }

}
?>

my_get_thumbnails function can be used also to

  • get the parent/children post thumbnails also for a post that is not the current, simply passing a post ID or a post object as first argument
  • get only the parent post thumbnail passing 'parent' as second argument
  • get only the children post thumbnails passing 'children' as second argument
share|improve this answer
    
Hey! Very much thank you for answering! I have tried the above code, and it didn't really work and I have to admit I don't understand much of the content. Could you explain what is done or perhaps make a dummie explenation for me ;). –  HannesH Mar 30 at 16:44
    
It worked perfectly!!!! Thanks you saved my day! :)) –  HannesH Mar 30 at 19:08
    
Hey! Is there a easy way to change from thumbnail to any other added image size. Like 'large'? Or do you have to change the original code? –  HannesH Mar 31 at 7:02
    
If I change that part to why doesn't it work? Than I have no array value, right? if ( ! empty( $thumb_ids ) ) { $children = wp_get_attachment_image_src( $thumb_ids); } } // if we want parent post thumbnail... if ( $which !== 'children' && $post->post_parent ) { // get the url of parent post thumbnail $parent = wp_get_attachment_image_src( get_post_thumbnail_id( $post->post_parent ) ); } –  HannesH Mar 31 at 7:37
    
You can't call wp_get_attachment_image_src passing an array. I updated the answer, see the new code and change 'large' with the size you want. @HannesH –  G. M. Mar 31 at 7:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.