Getting featured image of blog page rather than post image

admin2025-01-08  6

I have a page called "Blog" which uses "Default Template" (index.php). It has a featured image. I want to display this image, but when I use get_post_thumbnail_id(), inside or outside the loop, I get the thumbnail of the first post on my blog, not the one of my "Blog" page.

Just in case, in Settings > Reading, Posts Page is set as "Blog"

I have a page called "Blog" which uses "Default Template" (index.php). It has a featured image. I want to display this image, but when I use get_post_thumbnail_id(), inside or outside the loop, I get the thumbnail of the first post on my blog, not the one of my "Blog" page.

Just in case, in Settings > Reading, Posts Page is set as "Blog"

Share Improve this question asked Mar 20, 2014 at 11:50 drake035drake035 1,2177 gold badges34 silver badges57 bronze badges 1
  • Can you add the relevant template code please. – kraftner Commented Mar 20, 2014 at 11:51
Add a comment  | 

3 Answers 3

Reset to default 0

This code might be what you're after. This loops through page templates to grab the featured image. If you add it outside the loop in your page.php template (or index.php, depending on your theme), it should grab the pages' featured image, not the posts'.

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

        //get the ancestors
        $familyTree = get_ancestors($post->ID,'page');
        array_unshift( $familyTree, $post->ID ); //add the current page to the beginning of the list

        //loop through the family tree until you find a result or exhaust the array
        $featuredImage = '';
        foreach ( $familyTree as $family_postid ) {
            if ( has_post_thumbnail( $family_postid ) ) {
                $featuredImage = get_the_post_thumbnail( $family_postid, 'full' );
                break;
            }
        }

        // if the page has a featured image then show it
        echo ( $featuredImage ? $featuredImage : "" );

    }    
?>        

You should add this outside your loop

When you're on the Blog page, the function get_post_thumbnail_id() returns the post thumbnail id of the first post (as you already found out).

To get the correct post thumbnail on the blog page, you need to do something like this:

$thumbnail_id = NULL;
if(is_home()){  //blog page?
   $blog_page_id = (int)get_option('page_for_posts');
   if(has_post_thumbnail($blog_page_id)){
      $thumbnail_id = get_post_thumbnail_id(get_option('page_for_posts'));
   }
} elseif(is_singular()) { //everything 'single' like post, page, custom post type...
    if(has_post_thumbnail()){
        $thumbnail_id = get_post_thumbnail_id();
    }
} else {  //everything else like 404, category archives, search etc
   //put in a fallback here
}
if(!$thumbnail_id()){
   //put another fallback here
}
$thumbnail = get_attachment_image($thumbnail_id,'post-thumbnail');
$thumbnail_src = wp_get_attachment_image_src($thumbnail_id,'post-thumbnail')[0];

Happy Coding!

First, find post page id and pass id to wp_get_attachment_url(get_post_thumbnail_id()) function.

$blog_page_image = wp_get_attachment_url( get_post_thumbnail_id(get_option( 'page_for_posts' )) );

Hope this will fix your problem.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270103a1403.html

最新回复(0)