有时我会有 2 个链接或 1 个链接。
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
我想在两个链接之间添加“或”。因此,如果有 2 个链接,请添加“或” 例如,“Soundcloud 或 Youtube”
如果有 1 个链接,请不要添加“或”。例如,“Soundcloud”
<div class="container-wrap" id="music">
<?php
$args = array('post_type' => 'music-playlist-1');
$query = new WP_Query($args);
$cntr = 0;
while( $query -> have_posts() ) : $query -> the_post(); $cntr++; ?>
<section class="row-wrap">
<div class="row-inner">
<?php if ($cntr % 2 == 1) { ?>
<?php
$image = get_field('artwork');
if( !empty($image) ): ?>
<img class="artwork" src="<?php echo $image['url']; ?>">
<?php endif; ?>
<div class="artwork-content">
<h1><?php the_field('playlist-name'); ?></h1>
<button class="btn-wrap">
<div class="btn">listen now</div>
</button>
<div class="option-bar">
<?php
// check if the repeater field has rows of data
if( have_rows('playlist_link') ):
// loop through the rows of data
while ( have_rows('playlist_link') ) : the_row();
?>
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
<?php
endwhile;
else :
// no rows found
endif;
?>
</div>
</div>
<?php } else { ?>
<div class="artwork-content">
<h1><?php the_field('playlist-name'); ?></h1>
<button class="btn-wrap">
<div class="btn">listen now</div>
</button>
<div class="option-bar">
<?php
// check if the repeater field has rows of data
if( have_rows('playlist_link') ):
// loop through the rows of data
while ( have_rows('playlist_link') ) : the_row();
?>
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
<?php
endwhile;
else :
// no rows found
endif;
?>
</div>
</div>
<?php
$image = get_field('artwork');
if( !empty($image) ): ?>
<img class="artwork" src="<?php echo $image['url']; ?>">
<?php endif; ?>
<?php } ?>
</div>
</section>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
最佳答案
我从来没有真正使用过中继器行,但是查看文档我假设您可以检查如果 get_row_index()
为 0,您是否在第一行(因为似乎有无法检查您是否在最后一行)。因此:
编辑:好的,现在我已经试过了。对不起。 get_row_index()
是 5.3.4 版本中的新功能。也许这是你的问题?
这是一个没有get_row_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;
?>
编辑 2:我们如何使其可重用?
好的,我会用 super 简单的方法来做这件事。我获取所有代码,将其包装在一个函数中...并将其放入 functions.php
:
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(); ?>
如果您愿意,可以多次使用它。
(如果您只在单个页面模板上使用此函数,那么 functions.php
可能不是它的最佳位置,因为它会在任何地方加载。您可以直接在模板中创建函数-文件。)
关于php - 如何在 ACF 中为字段类型转发器附加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37516161/