我试图修改网站的内容标题,因此它会显示搜索结果页面的自定义背景图片以及帖子类型的单个帖子。这是当前的工作代码:
<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;"';
}
?>>
这就是我提出的:
<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; ?>>
但由于某些原因,这不起作用,页面和页面的其余部分不再加载。
我可能做错了什么,但不知道究竟是什么。所以任何帮助都表示赞赏。
也许是因为单笔资金是帖子类型而不是网页?所以也许我必须更改&#39; if( is_page_template ( &#39;单perspactive-funding.php&#39;))&#39;进入别的东西?
答案 0 :(得分:1)
您的语法存在问题,建议在开发阶段启用错误打印。 您的代码类似于
<?php
// Some code ...
<?php
// you cannot have php inside php
?>
?>
我已尝试修复以下代码中的语法错误
<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>
希望这有帮助。