有时我会有两个链接或一个链接。
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>我想添加“或”之间的两个链接。因此,如果有两个链接,添加“或”例如,“声音云或Youtube”
如果有一个链接,不要添加“或”。例如,“声音云”

<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(); ?>
发布于 2016-05-30 02:58:08
我从未实际使用过中继器行,但查看文档,我将假设您可以检查是否位于第一行,如果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:我们如何使它可重用?
好吧,我会用超级简单的方式来做。我把所有的代码打包成一个函数.把它放进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并不是最适合它的地方,因为它在任何地方都被加载。您可以直接在模板文件中创建函数。)
https://stackoverflow.com/questions/37516161
复制相似问题