I'm using the below code in my header.php file to have a different div depending on the page:
<?php
if ( is_singular( 'movie' ) ) {
echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>";
} elseif (has_post_thumbnail( $post->ID ) ) {
echo "<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?> <div class='heroImage' style='background-image:url(<?php echo $image[0]; ?>);'>";
} else {
echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>";
}
?>
The conditions for if ( is_singular( 'movie' ) ) {
and else {
work fine, but the one that checks if there's a featured image doesn't. The code in there is to get the URL of the featured image so it can insert it as the div's background image. I think the issue is the echo inside an echo?
When I look at the code it outputs in my browser, it looks like this:
<div class="heroImage" style="background-image:url(<?php echo ; ?>);">
I've tried and looked online to help fix it, but can't seem to find a solution. Appreciate any help.
PHP won't execute within a string. Simply assign the $image
variable before echo-ing the string
elseif (has_post_thumbnail($post->ID)) {
$image = wp_get_attachment_image_src(
get_post_thumbnail_id($post->ID), 'full');
echo '<div class="heroImage" style="background-image:url(', $image[0], ')">';
}
Try assigning the $image
variable before echoing the string.
Ie
<?php
if ( is_singular( 'movie' ) ) {
echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>";
} elseif (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo "<div class='heroImage' style='background-image:url" . $image[0] . ')">' ;
} else {
echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>";
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.