当您在博客页面上时,函数get\\u post\\u thumbnail\\u id()返回第一篇文章的文章缩略图id(正如您已经发现的那样)。
要在博客页面上获得正确的帖子缩略图,您需要执行以下操作:
$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];
快乐的编码!