简体   繁体   English

PHP wordpress模板-如果未选择,则添加默认图像

[英]PHP wordpress template - add default image if none selected

Apologies, I am relatively new to PHP and am learning as I go, but am stuck on this... I have this template for a page-title section with a background image. 抱歉,我对PHP比较陌生,正在学习,但是仍然受此困扰。。。我有一个带有背景图片的页面标题部分的模板。 I can add the background image, but if there is no background-img:url elected on the page, is there a way to write in a default background-img:url into this template? 我可以添加背景图片,但是如果页面上没有选举出background-img:url,是否可以将默认的background-img:url写入此模板中?

<section class="page-title" <?php if($bg):?>style="background-image:url('<?php echo esc_url($bg);?>');"<?php endif;?>>
    <div class="auto-container">
        <h1><?php if($title) echo balanceTags($title); else wp_title('');?></h1>
        <div class="bread-crumb">
            <?php echo convo_get_the_breadcrumb();?>
        </div>
    </div>

Add this before your code: 在代码之前添加此代码:

if(empty($bg)){
    $bg = '/path/to/your/default/image.jpg';
}
<section class="page-title" <?php if($bg):?>style="background-image:url('<?php echo esc_url($bg);?>')" <?php  else: ?>
style="background-image:url('your URL')"
<?php endif; ?>>

Using @psdev's code, I was able to add in a default background-image if $bg was empty. 使用@psdev的代码,如果$ bg为空,我可以添加默认的背景图像。 Thanks!! 谢谢!!

<section class="page-title" <?php if(empty($bg)){$bg = 'http://aqmenalt.mhsites.co.uk/wp-content/uploads/2017/06/Banner-10.png';} if($bg):?>style="background-image:url('<?php echo esc_url($bg);?>');"<?php endif;?>>
    <div class="auto-container">
        <h1><?php if($title) echo balanceTags($title); else wp_title('');?></h1>
        <div class="bread-crumb">
            <?php echo convo_get_the_breadcrumb();?>
        </div>
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM