簡體   English   中英

在ACF轉發器字段中使用發布對象

[英]Using post object inside ACF repeater field

我在網站上使用“高級自定義字段”。

我有一個名為anime_par的轉發器字段,而子字段為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對象。

我在您的字段名稱中添加了一些完全可選的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 ,因為它在documenration說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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM