-1

准菜鸟在这里。我只是无法从 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; ?>
4

1 回答 1

1

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

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

<ul>
<?php foreach (get_field('topic') as $row) :?>
    <li><?php print $row['topic_title'] ?></li>                  
<?php endforeach; ?>
</ul>
于 2013-06-27T18:53:39.160 回答