Conditional background image in header

▼魔方 西西 提交于 2019-12-11 09:29:54

问题


I'm trying to modify the content-header of a website, so it will display a custom background image for the search result page and for single posts of a post type. Here's the current working code:

<div class="single__header" <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo 'style="background-image: url(\'';
the_post_thumbnail_url('post-background'); 
echo '\'); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
}
?>>

And this what I've come up with:

<div class="single__header" 
<?php if (is_page_template('single-perspactive-funding.php')) {
        echo 'style="background-image: url([website-url-hidden]/content/uploads/2018/04/single-funding-header.jpg); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
}
?>
<?php elseif (is_search()) {
        echo 'style="background-image: url([website-url-hidden]/content/uploads/2018/04/Header-image_Notfound.jpg); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
}
?>
<?php else : ?>
  <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
            echo 'style="background-image: url(\'';
            the_post_thumbnail_url('post-background'); 
            echo '\'); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
          }
          ?>
<?php endif; ?>>

But for some reason this isn't working, the header and the rest of the page is no longer loading.

I'm probably doing something wrong, but don't know exactly what. So any help is appreciated.

Maybe it's because the single funding is a post type and not a page? So maybe I have to change 'if (is_page_template('single-perspactive-funding.php'))' into something else?


回答1:


There is an issue with your syntax, it is recommended to enable error printing in the development phase. Your code is something like

<?php
// Some code ...
   <?php
   // you cannot have php inside php
   ?>
?>

I have tried to fix the syntactical errors in below code

<div class="single__header" 
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    $bgsearch = 'style="background-image: url([website-url]/content/uploads/2018/04/Header-image_Notfound.jpg); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
    $bgdefault = 'style="background-image: url(\''.the_post_thumbnail_url('post-background').'\'); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"'; 

    if (is_search()) {
        echo $bgsearch;
    } else {
        echo $bgdefault;
    }    
}

?>
>
    Div contents
</div>

Hope this helps.



来源:https://stackoverflow.com/questions/49823392/conditional-background-image-in-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!