admin管理员组文章数量:1418610
I currently have in my header.php:
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'header-large' ); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div id="wrapper-navbar" itemscope itemtype="" style="background-image: url('<?php echo $backgroundImg[0]; ?>');">
<?php } else { ?>
<div id="wrapper-navbar" itemscope itemtype="">
<?php } ?>
Which sets the background image if there is a featured image on the pages and posts. If there is none then there is a fall back where I have a default background image.
My problem arises on the Blog page - The page which is set as the "Posts Page'
The background image displayed on the "Posts Page" is the featured image of the latest Post on the list, NOT the featured image I have set for the page.
How do I display the featured image of the Page and not of the latest post on the list?
I currently have in my header.php:
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'header-large' ); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div id="wrapper-navbar" itemscope itemtype="http://schema/WebSite" style="background-image: url('<?php echo $backgroundImg[0]; ?>');">
<?php } else { ?>
<div id="wrapper-navbar" itemscope itemtype="http://schema/WebSite">
<?php } ?>
Which sets the background image if there is a featured image on the pages and posts. If there is none then there is a fall back where I have a default background image.
My problem arises on the Blog page - The page which is set as the "Posts Page'
The background image displayed on the "Posts Page" is the featured image of the latest Post on the list, NOT the featured image I have set for the page.
How do I display the featured image of the Page and not of the latest post on the list?
Share Improve this question asked Jul 25, 2019 at 5:17 iamonstageiamonstage 1671 silver badge9 bronze badges1 Answer
Reset to default 1You might be able to do this by calling wp_reset_postdata();
otherwise you will need to get the blog page ID and set that on the line before instead:
if ( is_home() ) {$post_id = get_option( 'page_for_posts' );} else {$post_id = $post->ID;}
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'header-large' ); ?>
Note the is_home()
is actually used for the blog page, as distinct from is_front_page()
which is actually for the "homepage".
本文标签: How to get the Page featured imagenot the Post featured image
版权声明:本文标题:How to get the Page featured image, not the Post featured image 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745297633a2652173.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论