在ACF中继器字段中使用post对象


Using post object inside ACF repeater field

我在我的网站上使用高级自定义字段。

我有一个名为anime_par的中继器字段,其中sub_field名为animateur。子字段animateur是post-object。

我在我的页面中使用了这个循环,这个循环显示了自定义帖子类型中某个类别的帖子。

我想做的是在我的页面中显示animateur选择的帖子名称和帖子链接。

这是我使用的代码,但它不工作,它显示了我当前页面的永久链接,而不是在自定义字段中选择的那个。

<?php while(has_sub_field('anime_par')): ?>
<a href="<?php echo get_permalink('the_sub_field("animateur")'); ?>"><?php echo get_title('the_sub_field("animateur")'); ?></a>
<?php endwhile; ?>

有什么建议吗?

谢谢你的帮助,

这个方法对我来说是有效的,根据ACF上的中继器和post对象文档。您必须在repeater循环中设置post对象。

我添加了您的字段名,以及一些完全可选的html来显示结构。

希望有帮助。

<!-- Start Repeater -->
<?php if( have_rows('anime_par')): // check for repeater fields ?>
<div class="a-container">
    <?php while ( have_rows('anime_par')) : the_row(); // loop through the repeater fields ?>
    <?php // set up post object
        $post_object = get_sub_field('animateur');
        if( $post_object ) :
        $post = $post_object;
        setup_postdata($post);
        ?>
    <article class="your-post"> 
        <?php the_title(); ?>
        <?php the_post_thumbnail(); ?>
        <?php // whatever post stuff you want goes here ?>
    </article>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?> 
    <?php endwhile; ?>
</div>
<!-- End Repeater -->
<?php endif; ?>

the_sub_field离不开has_sub_field你要做的就是使用循环和has_sub_field就像文档中说的http://www.advancedcustomfields.com/resources/functions/the_sub_field/

或者你可以像这样使用get_field('repeater_sluf')

$rows = get_field('repeater_field_name' ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['sub_field_name' ]; // get the sub field value 
<?php if(get_field('favourite_design_quarters', 'user_'.$current_user->ID)): ?>

<?php while(has_sub_field('favourite_design_quarters', 'user_'.$current_user->ID)): 
$company = get_sub_field('company_name');
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $company->ID ), 'package-thumbnail' ); 
?>
                <tr>
                    <td><img src="<?php echo $image[0]; ?>" alt="<?=$company->post_title;?>" /></td>
                    <td><?=$company->ID;?></td>
                    <td style="text-align:left;"><?=$company->post_content;?></td>
                    <td><?=$company->post_date;?></td>
                    <td><a href="#">Delete</a></td>
                </tr>