1

我希望有人能帮助我。我在使用 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>';
 } ?>

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

谢谢!

4

2 回答 2

1

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

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

于 2014-02-12T13:48:08.617 回答
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>';
 } ?>

当循环通过这些字段之一时,此函数从当前行返回一个子字段。

于 2014-02-12T09:52:17.653 回答