简体   繁体   English

在ACF转发器字段中使用发布对象

[英]Using post object inside ACF repeater field

I'm using Advanced Custom Fields on my website. 我在网站上使用“高级自定义字段”。

I have a repeater field called anime_par, with sub_field called animateur. 我有一个名为anime_par的转发器字段,而子字段为animateur。 Sub field animateur is a post-object. 子场动画是后对象。

I'm using this inside a loop in my page, a loop that displays posts from a category inside a custom post type. 我在页面的循环中使用此循环,该循环显示自定义帖子类型中某个类别的帖子。

What I'm trying to do is to display the post name and post link of the animateur selection inside my page. 我想做的是在页面中显示动画名称选择的帖子名称和帖子链接。

Here is the code I'm using but it's not working, it displays the permalink of my current page, not the one selected in the custom field. 这是我正在使用的代码,但无法使用,它显示了我当前页面的固定链接,而不是在自定义字段中选择的那个。

<?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; ?>

Any suggestions to make this work? 有什么建议可以使这项工作吗?

thanks for your help, 谢谢你的帮助,

This method is working for me, per the repeater and post object docs on ACF. 根据ACF上的转发器和发布对象文档,此方法对我有用。 You've got to set up the post object inside of the repeater loop. 您必须在转发器循环中设置post对象。

I added in your field names, and some completely optional html to show the structure. 我在您的字段名称中添加了一些完全可选的html来显示结构。

Hope it helps. 希望能帮助到你。

<!-- 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 doesn't work without has_sub_field What you have to do it is use loop with has_sub_field as it said in the documenration http://www.advancedcustomfields.com/resources/functions/the_sub_field/ the_sub_field不工作没有has_sub_field你所要做的是使用循环与has_sub_field ,因为它在documenration说http://www.advancedcustomfields.com/resources/functions/the_sub_field/

or you can use get_field('repeater_sluf') like that 或者你可以像这样使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM