【问题标题】:Advanced Custom Fields in Wordpress - "if" statement troublesWordpress 中的高级自定义字段 - “if”语句的麻烦
【发布时间】:2014-03-10 11:43:40
【问题描述】:

我希望有人可以帮助我。我在使用 Wordpress 的高级自定义字段插件的 if 语句时遇到问题。我有三个选项可供用户选择,三个都可以选择,但如果他们愿意,他们也可以只选择一个。

我遇到的问题是我编写的代码显示了所有的 HTML 标记,甚至是空的标记。这会导致样式问题。我希望能够只显示已填充的 HTML。我在 ACF 论坛上尝试了解决方案,但无济于事。

链接:http://www.advancedcustomfields.com/resources/getting-started/code-examples/

这是我现在得到的快速(新手!)代码:

<a href="<?php the_sub_field('link'); ?>"><?php the_sub_field('link'); ?></a>
<a href="<?php the_sub_field('doc'); ?>"><?php the_sub_field('doc'); ?></a>
<p><?php the_sub_field('cap'); ?></p>

我查看了 ACF 论坛并尝试了这个,但它打破了主题:

<?php if(the_sub_field('link')) {
    echo '<a href="' . the_sub_field('link') . '">' . the_sub_field('link') . '</a>';
} ?>

<?php if(the_sub_field('doc')) {
    echo '<a href="' . the_sub_field('doc') . '">' . the_sub_field('doc') . '</a>';
} ?>

 <?php if(the_sub_field('cap')) {
     echo '<p>' . the_sub_field('cap') . '</p>';
 } ?>

我正在寻求一些帮助来完成这项工作。我不认为我离正确答案太远了,但是我有点像新手,除了标准的前端东西之外,任何想法都会非常感激。

谢谢!

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    尝试使用get_sub_field();

    <?php if(get_sub_field('link')) {
        echo '<a href="' . the_sub_field('link') . '">' . the_sub_field('link') . '</a>';
    } ?>
    
    <?php if(get_sub_field('doc')) {
        echo '<a href="' . the_sub_field('doc') . '">' . the_sub_field('doc') . '</a>';
    } ?>
    
     <?php if(get_sub_field('cap')) {
         echo '<p>' . the_sub_field('cap') . '</p>';
     } ?>
    

    当遍历这些字段之一时,此函数从当前行返回一个子字段。

    【讨论】:

      【解决方案2】:

      就像 Dk-Macadamia 所说,尝试在循环中使用 get_sub_field() 而不是 the_sub_field() 不同的是get_sub_field()将值作为字符串返回,the_sub_field()打印数据,

      另外get_sub_field() 只能在中继器/流体场类型下工作,否则将无法工作, 如果它不是中继器/流体字段的子字段,请尝试get_field()

      【讨论】:

        猜你喜欢
        • 2015-09-27
        • 2012-01-17
        • 1970-01-01
        • 1970-01-01
        • 2014-10-13
        • 1970-01-01
        • 1970-01-01
        • 2012-08-10
        • 1970-01-01
        相关资源
        最近更新 更多