【问题标题】:PHP prints text instead of HTMLPHP 打印文本而不是 HTML
【发布时间】:2020-01-31 20:07:24
【问题描述】:

我有一个自定义帖子类型“书”和两个自定义字段作者和评分。速率可以从 1 到 5,我想打印字体真棒星形图标的次数与速率字段的数量一样多。但是当我回显字体真棒图标时,它只打印文本,而不是图标本身。

代码:

<div class="book">
        <h3>“<?php the_title(); ?>”</h3>
        <p><?php the_field('author'); ?></p>
        <?php 
            for ($i = 0; $i < the_field('rate'); $i++)
                echo '<i class="fas fa-star"></i>';
        ?>
 </div>

【问题讨论】:

  • 还需要您提供自定义函数(the_title()the_field()),我们无法验证它们返回的内容。

标签: php wordpress advanced-custom-fields custom-post-type


【解决方案1】:

好像你有错字。班级应该是“fa fa-star”

<div class="book">
        <h3>“<?php the_title(); ?>”</h3>
        <p><?php the_field('author'); ?></p>
        <?php 
            for ($i = 0; $i < the_field('rate'); $i++)
                echo '<i class="fa fa-star"></i>';
        ?>
 </div>

【讨论】:

    【解决方案2】:

    问题出在 the_field 函数中。它会立即打印数字,因此当 for 迭代开始时,函数会打印值并停止循环。我用返回值的 get_field 函数替换了 the_field,现在它可以工作了!

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多