【问题标题】:HTML not showing inside IF statement (Wordpress)HTML 未在 IF 语句中显示(Wordpress)
【发布时间】:2013-10-23 07:51:03
【问题描述】:

我有以下问题。我正在使用 Wordpress 的高级自定义字段为帖子创建字幕字段。我喜欢给这个副标题一些样式,但是我在 IF 语句中的 HTML 代码没有显示在页面上。 $subtitle 会显示。

<?php $subtitle = the_field('subtitle'); ?>
<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

我花了几个小时寻找类似的问题,但找不到任何解决方案。所以这可能是我的菜鸟错误。

解决方案

<?php $subtitle = get_field('subtitle'); ?>
<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

将 the_field() 更改为 get_field()。向 Aditya Vikas 致敬!

【问题讨论】:

    标签: php wordpress if-statement


    【解决方案1】:

    您应该回显/打印它(字幕变量)。

    <div class="post-sub-title"><?php echo $subtitle; ?></div>
    

    【讨论】:

    • 好的,感谢您的回复,但这并不能解决 HTML 未显示的问题。
    • 那我估计调用trim函数后subtitle中的数据长度不大于零。或者您的代码中的其他地方一定有错误,而不是您提供的部分。 编辑:尝试不使用if语句打印它,看看它是否可以打印。
    【解决方案2】:

    尝试使用这段代码:

    <?php if(strlen(trim($subtitle)) > 0): ?>
      <div class="post-sub-title"><?=$subtitle?></div>
    <?php endif; ?>
    

    而不是这个:

    <?php if(strlen(trim($subtitle)) > 0): ?>
      <div class="post-sub-title"><?php $subtitle; ?></div>
    <?php endif; ?>
    

    还有一件事!

    the_field() 不是 WordPress 的默认功能

    您使用的“随便”插件可能有相应的功能:

    get_field()

    【讨论】:

    • 我尝试了你的建议。但它没有用。
      仍然没有显示。只有变量 $subtitle(包含一些纯文本)可以。
    • 将 the_field() 更改为 get_field() 已解决问题!非常感谢!
    【解决方案3】:

    试试这个:

    <?php $subtitle = get_field('subtitle'); ?>
    <?php if(!empty(trim($subtitle))): ?>
      <div class="post-sub-title"><?php $subtitle; ?></div>
    <?php endif; ?>
    

    谢谢。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签