0

I want the featured image of a post to display not only in the blog list view, but also inside the post (when i press "Read More..."). How do i do that?

I pasted this

if ( has_post_thumbnail() ) { 
 the_post_thumbnail('full');
} 

in single.php just before the_post();. It looks like this:

while (have_posts()) {
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
      the_post_thumbnail('full');
    } 
    the_post();
    get_template_part('content', 'single');
}

But the image shows up, above the title. How do i make it show after the title, just like in the blog list view?

2 Answers 2

3

@PatJ's answer affords the most control over the placement of the image but involves editing the theme, which may or may not be wise, or possible, depending on the circumstance, so an alternative is to add a filter to the_content.

function add_thumb_wpse_100914($content) {
    // check that we are on a 'single' post display and...
    // check if the post has a Post Thumbnail assigned to it.
    if ( is_single() && has_post_thumbnail() ) { 
      $content = get_the_post_thumbnail(null,'full').$content;
    } 
    return $content;
}
add_filter('the_content','add_thumb_wpse_100914');
5
  • Where exactly do i paste this code? Commented May 27, 2013 at 17:51
  • @user33351 in functions.php of your current theme Commented May 27, 2013 at 17:51
  • Well, now the image does show up inside the post, but it has a side effect. The image shows twice in the blog list view. How can i fix that? Commented May 27, 2013 at 17:57
  • You can put that in functions.php, in a plugin file, or in a must-use plugin. To fix the double image in the blog list, add an is_single condition. I'll edit the answer.
    – s_ha_dum
    Commented May 27, 2013 at 17:58
  • Very nice! Thanks man. That is exactly what i wanted. :) Commented May 27, 2013 at 18:00
1

There should be a file in your theme called content-single.php (if it's not there, look instead for content.php). Inside that file there will be a line like the_title(). If you put your the_post_thumbnail( 'full' ); after that line, it should appear after the title in the single page view.

Rationale

the_post() sets up the data for the next post in The Loop (in the case of a single page, it sets up the post/page). The next line in your single.php -- get_template_part() -- is where the post/page view is actually assembled; it should contain the_title(), the_content(), etc.

References

Codex:
the_post()
get_template_part()
the_title()

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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