dongyi2889 2014-02-18 02:21
浏览 72

Wordpress和PHP检查自定义字段是否存在,如果不是随机字符串替换

I'm trying to create a function that checks for the existence of a post_thumbnail() for a post in Wordpress and sets an element's background image appropriately. Here's what I've tried:

 <div class="header-cover-image" style="background-image: url(
    <?php if ( has_post_thumbnail() ) {
      the_post_thumbnail('full');
    } else { echo get_template_directory_uri() . '/assets/img/article-overlay-1.jpg' }?>
    ">
  </div>

This currently crashes/causes the page to render nothing at all. I'm so confused about when and where to use quotation marks when using PHP within HTML markup!

Any ideas how I can make this work?

  • 写回答

2条回答 默认 最新

  • dtoqa66028 2014-02-18 02:46
    关注

    There was a problem with trying to echo the Wordpress function and append the string simultaneously. Fixed it by doing:

    <?php if ( has_post_thumbnail() ) {
      the_post_thumbnail('full');
    } else { $uri = get_template_directory_uri(); echo $uri . '/assets/img/article-overlay-1.jpg'; }?>
    ">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大