如何在 ACF 中为字段类型转发器附加文本?

How do I append text in ACF for field type repeater?

有时我会有 2 link 或 1 link。

<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>

我想在 2 个 link 之间添加 "or"。所以如果有2个link就加上"or" 例如,"Soundcloud or Youtube"

如果有 1 个 link 就不要添加 "or"。例如,"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:我们如何使其可重复使用?

好的,我会用超级简单的方法来做这件事。我获取所有代码,将其包装在一个函数中...并将其放入 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 可能不是它的最佳位置,因为它到处都会加载。您可以直接在模板文件中创建函数。 )