0

I have a page "Blog" and it's set to display latest posts. This page itself has a thumbnail, and I want it to be displayed in header. Every other page/post has same layout and thumbnail shows in header just fine. And I managed to make it somehow work on "Blog" page also.

Here is my code:

<?php global $post;

if ( is_home() ) {
    $currentpageid = get_option( 'page_for_posts' );
} else {
    $currentpageid = $post->ID;
}
$page_thumb = wp_get_attachment_image_src( get_post_thumbnail_id($currentpageid), 'full' ); ?>

<header <?php if ( has_post_thumbnail() ) { ?> style="background-image: url(<?php echo $page_thumb['0'];?>)"<?php }?>></header>

Now the strange thing is that the "Blog" page's thumbnail shows up in header only if latest post in the loop also has a thumbnail. What could be the reason?

EDIT

well, $currentpageid = get_option( 'page_for_posts' ); actually returns a correct page id in this case, I can echo it anywehere. But when i try to pass it to any function - it doesn't work

$page_thumb = wp_get_attachment_image_src( get_post_thumbnail_id($currentpageid), 'full' );
  • A quick test indicated that this should work. I can only assume that I have duplicated your setup correctly. Please explain the problem in more detail. – s_ha_dum Mar 9 '14 at 0:50
  • @s_ha_dum thanks for stopping by. well, it does work ONLY if the newest post in the loop (which is displayed right after the header) has a featured thumbnail image. In this case all thumbnails show up correctly. But if I delete a thumbnail for the newest post in the loop, the thumbnail of the 'latest posts' page also disappears. – Nikita Mar 9 '14 at 1:06
  • I don't see that behavior. The current post in the loop doesn't seem to matter. There is something happening that I am not duplicating. – s_ha_dum Mar 9 '14 at 1:07
  • @s_ha_dum humph, that's weird. I'm now clearing the rest of my markup piece by piece to find what could cause such behaviour. – Nikita Mar 9 '14 at 1:22
  • anyway, good to know that this code if fine, thanks @s_ha_dum – Nikita Mar 9 '14 at 1:24
0

Ok, here is how the thumbnail url should be called:

<header <?php if ( has_post_thumbnail($currentpageid) ) { ?> style="background-image: url(<?php echo $page_thumb['0'];?>)"<?php }?>></header>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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