0

In my head this sound right. I check if the post has a thumbnail. If it has, I assign the post thumbnail to a variable that I call as the source for my background image. If the post does not have a thumbnail, I assign a placeholder image as the background.

However, I'm having some issues :/

The code block below breaks my site, and if I change the else statement to $large_image_url = bloginfo('template_directory')?>/library/images/default-header-image.jpg<?php; it actually echoes out the variable as a line of text instead of saving it in the variable.

<?php 
if ( has_post_thumbnail() ) {
    $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
} else {
    $large_image_url = bloginfo('template_directory')'/library/images/default-header-image.jpg';
} ?>

<section class="header-image" style="background-image: url('<?php echo $large_image_url[0] ?>');">
</section>

Can someone give me a push in the right direction?

1

The bloginfo() function outputs immediately. since you're storing to a $variable you'll want to use get_bloginfo() instead. also, you're missing a concatenation.

$large_image_url = get_bloginfo('template_directory') . '/library/images/default-header-image.jpg';
2
  • Thank you @karpstrucking! That was indeed the issue :) I ran into another issue though, I'm echoing $large_image_url[0] and that doesn't seem to work for the bloginfo. I'm thinking it's becuase the thumbnail is an object and originally i'm extracting the url from that image.. Can I somehow make that spesific to the thumbnail? – turtledropbomb Nov 25 '14 at 17:34
  • try adding $large_image_url = array( $large_image_url ); to the else {} (after the get_bloginfo line). this should convert it to an array with the url as the first key – karpstrucking Nov 25 '14 at 17:42

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.