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.

Hej! I have a subpage to display it's subpage featured image. I would like to add the title and description to beneath the thumbnail. Today my code look like this >

In the functions.php

function my_geturls($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_src( $tid ); // <-- 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 ( $img ) {
  // get the url of parent post thumbnail
  $urls = wp_get_attachment_src( $tid );  // <-- 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 );
}

This is my code in the page.php file.

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

      $thumbnails = my_get_thumbnails();

      if ( ! empty( $thumbnails['parent'] ) ) {
        $format = '<a href="<?php echo get_page_link( $child_page->ID ); ?><div class="parent-thumb"><img src="%s" class="img-responsive" id="img-center" max-height="255px" width="auto" alt="" /></div></a>';
        printf( $format, $thumbnails['parent'] );
      }

      if ( is_array( $thumbnails['children'] ) && ! empty( $thumbnails['children'] ) ) {
        $open = '<a href="<?php echo get_page_link( $child_page->ID ); ?>"><div max-height="255px" width="auto" class="child-thumb"><img class="img-responsive" id="img-center" max-height="255px" width="auto" src="';
        $close = '" alt="" /></div><a>';
        echo $open . implode( $close . $open, $thumbnails['children'] ) . $close;
      }

    }
    ?>

Thanks in advance! Hannes

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I found how it could be done! Instead of using that function it was possible to add a query and another function. This is the code goes in the page.php file.

                <?php $subs = new WP_Query( array( 'post_parent' => $post->ID, 'post_type' => 'page', 'meta_key' => '_thumbnail_id' ));
    if( $subs->have_posts() ) : while( $subs->have_posts() ) : $subs->the_post();
    echo '<div class="re-featured-image child-thumb">' . get_the_post_thumbnail($post->ID, 'index-thumb', array('class' => 'img-responsive', 'id' => 'img-center'));
    echo '<div class="re-featured-image-title">' . get_post(get_post_thumbnail_id($post->ID))->post_title . '</div>';
    echo '<div class="re-featured-image-caption">' . get_post(get_post_thumbnail_id($post->ID))->post_excerpt . '</div>';
    echo '<div class="re-featured-image-description">' . get_post(get_post_thumbnail_id($post->ID))->post_content . '</div></div>';

    endwhile; endif; wp_reset_postdata(); ?>

If you want your thumbnail to link to it's page add this function in the functions.php file. I found the code here http://wordpress.org/support/topic/making-post-thumbnail-link-to-post

add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

function my_post_image_html( $html, $post_id, $post_image_id ) {

$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';

return $html;

}

share|improve this answer

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.