首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ACF中添加字段类型中继器的文本?

如何在ACF中添加字段类型中继器的文本?
EN

Stack Overflow用户
提问于 2016-05-30 00:17:16
回答 1查看 1.4K关注 0票数 3

有时我会有两个链接或一个链接。

代码语言:javascript
复制
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>

我想添加“或”之间的两个链接。因此,如果有两个链接,添加“或”例如,“声音云或Youtube”

如果有一个链接,不要添加“或”。例如,“声音云”

代码语言:javascript
复制
<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(); ?>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-30 02:58:08

我从未实际使用过中继器行,但查看文档,我将假设您可以检查是否位于第一行,如果get_row_index()为0(因为似乎无法检查是否位于最后一行)。因此:

编辑:好的,现在我已经试过了。抱歉的。get_row_index()是5.3.4版中的新版本。也许这就是你的问题?

这里有一个没有get_row_index()的解决方案

我在添加的每一行之前都添加了+

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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

然后,在模板文件中,可以将所有代码替换为

代码语言:javascript
复制
<?php playlist_links(); ?>

如果你愿意的话,多用几次。

(如果您只在单个页面模板上使用此函数,那么也许functions.php并不是最适合它的地方,因为它在任何地方都被加载。您可以直接在模板文件中创建函数。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37516161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档