Reputation: 147
I have created an if statement to check if the current wordpress page/post has a feature image set. If it does not I want the script to echo the URI for the placeholder image. The script run's without any PHP errors however when no feature image is available it echos the following background: url(h)
and not the URI for the placeholder. I can see the issue is with the inline echoing $image[0]
however I don't know how to fix this.
<?php if(is_home()) {
$image = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full');
} elseif (has_post_thumbnail($post->ID)) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail, page_for_posts' );
} else {
$image = get_template_directory_uri() . '/assets/images/placeholder.png';
}
?>
<div class="hero-inner" style="background: url(<?php echo $image[0]; ?>) no-repeat center center; background-size: cover;">
[...]
</div>
Upvotes: 0
Views: 38
Reputation: 147
Changing the var $image to $image[0] for the last else works.
<?php if(is_home()) {
$image = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full');
} elseif (has_post_thumbnail($post->ID)) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail, page_for_posts' );
} else {
$image[0] = get_template_directory_uri() . '/assets/images/placeholder.png';
}
?>
<div class="hero-inner" style="background: url(<?php echo $image[0]; ?>) no-repeat center center; background-size: cover;">
[...]
</div>
Upvotes: 0
Reputation: 1662
if(is_home()) {
$image = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full');
} elseif (has_post_thumbnail($post->ID)) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail, page_for_posts' );
}
if($image){
$image = $image[0];
}
else {
$image = get_template_directory_uri() . '/assets/images/placeholder.png';
}
?>
<div class="hero-inner" style="background: url(<?php echo $image; ?>) no-repeat center center; background-size: cover;">
[...]
</div>
Upvotes: 1