WordPress高级自定义字段重复字段- get_field不为我工作


WordPress Advanced Custom Fields repeater field -- get_field not working for me

这里有点像新手。我不能从get_field得到任何结果。下面是我的测试页面:

    http://23.21.199.240/test

我可以使用FOR循环检索中继器字段数据,但是特殊的ACF get_field函数没有按照我的期望进行操作。(这看起来很简单,所以如果我犯了一个完全愚蠢的错误,我也不会感到惊讶。)

任何帮助都将非常感激!

<?php
/*
Template Name: test
*/
?>
<?php get_header(); ?>
<?php query_posts('category_name=worldwise&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    id: <?php the_ID(); ?><br />
    <!--let's try to display all the topics (an ACF repeater field) for this post....-->
    <?php if(get_field('topic')): // if there are topics, get all their data in an array ?>
        <?php
            $topic_list_1 = ''; // set up an empty topic list
            while(has_sub_field('topic')): // for every topic 
                $topic_title_1 = get_sub_field('topic_title'); // get the title
                $topic_list_1 .= '<li>' . $topic_title_1 . '</li>'; // and add it to the topic list
            endwhile;  // no more topics, move on
        ?>
        <p><strong>The FOR loop produced these topics:</strong></p>
        <ul><?php echo $topic_list_1; ?></ul>
    <?php else: ?>
        <p style="color:red">GET_FIELD did not find any topics</p>
    <?php endif; ?>
    <!--or, let's try displaying those topics another way....-->
    <?php
        $vid_id = $post->ID;
        $vid_custom = get_post_custom($vid_id);
    ?>
    <?php if($vid_custom['topic'][0] != null): ?>
        <?php
            $topic_list_2 = '';
            for($r=0;$r<$vid_custom['topic'][0];$r++):
                $topic_title_2 = $vid_custom[topic_.$r._topic_title][0];
                $topic_list_2 .= '<li>' . $topic_title_2 . '</li>';                 
            endfor;
        ?>
        <p><strong>The FOR loop produced these topics:</strong></p>
        <ul><?php echo $topic_list_2; ?></ul>
    <?php else: ?>
        <p style="color:red">The FOR loop did not find any topics</p>
    <?php endif; ?>
    <br />
<?php endwhile; else: ?>
    <p>Sorry, no posts or pages matched your criteria.</p>
<?php endif; ?>

我不喜欢使用get_sub_field方法,因为当您嵌套重复器字段时,它会变得有点复杂。

在我看来,这是获得你想要做的事情的最简单的方法:

<ul>
<?php foreach (get_field('topic') as $row) :?>
    <li><?php print $row['topic_title'] ?></li>                  
<?php endforeach; ?>
</ul>