Php 如何在ACF中为字段类型转发器追加文本?
有时我会有2个链接或1个链接Php 如何在ACF中为字段类型转发器追加文本?,php,html,css,wordpress,advanced-custom-fields,Php,Html,Css,Wordpress,Advanced Custom Fields,有时我会有2个链接或1个链接 <a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a> 我想在两个链接之间添加“或”。因此,如果有2个链接,请添加“或” 例如,“Soundcloud或Youtube” 如果有1个链接,不要添加“或”。例如,“Soundcloud” "> 听着 听
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
我想在两个链接之间添加“或”。因此,如果有2个链接,请添加“或”
例如,“Soundcloud或Youtube”
如果有1个链接,不要添加“或”。例如,“Soundcloud”
">
听着
听着
">
我从来没有实际使用过repeater行,但是查看文档,我假设如果
get\u row\u index()
为0,您可以检查自己是否在第一行(因为似乎无法检查您是否在最后一行)。因此:
编辑:好的,现在我已经试过了。对不起<代码>获取行索引()在5.3.4版中是新的。也许这是你的问题
这是一个没有get\u row\u index()
我在添加的每一行之前添加了+
<?php
// check if the repeater field has rows of data
if (have_rows('playlist_link')):
+ // We set a flag for the first row
+ $is_first = true;
// loop through the rows of data
while (have_rows('playlist_link')) : the_row();
+ if ($is_first) :
+ $is_first = false;
+ else :
+ echo " OR ";
+ endif; ?>
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
<?php
endwhile;
else :
// no rows found
endif;
?>
然后在模板文件中,您可以用替换所有代码
<?php playlist_links(); ?>
如果你愿意的话,可以用几次
(如果您只在单页模板上使用此函数,那么可能
functions.php
不是它的最佳位置,因为它到处都可以加载。您可以直接在模板文件中生成函数。)谢谢您的回答,但它不起作用。我应该把代码放在?在我输入代码后,播放列表链接不会显示,其他音乐描述和标题、艺术作品也不会显示。让我知道这是否正确。啊,我的错!对不起,在回答之前我应该先测试一下!我对它进行了一些编辑,以便只显示重要的部分。(我仍然认为它应该可以工作!)你的意思是在之前添加代码吗?你能告诉我整个过程吗?我试过了,但没用:(我有两个while循环,哪个while循环?
function playlist_links() { // <--This line is new
if (have_rows('playlist_link')):
$is_first = true;
while (have_rows('playlist_link')) : the_row();
if ($is_first) :
$is_first = false;
else :
echo " OR ";
endif; ?>
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
<?php
endwhile;
endif;
} // <--This line is also new
<?php playlist_links(); ?>