从ACF灵活内容中获取最后一行并将其显示在另一页上

Yag*_*nte 6 wordpress advanced-custom-fields

我做了两页,一个首页和一个"基本内容页面".
在这个"基本内容页面"上,我制作了一个包含不同文本和图像的灵活内容.

我搜索一种显示首页最后一行的方法,是否可能?

更新:这是最后一个代码,由于@Nick Surmanidze,它可以使用"post object field"(名为"relation")从另一个页面获取内容.只留下如何抓住最后一排的问题.

<?php
$post_object = get_field('relation');

if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
        <div>
                <?php
// check if the flexible content field has rows of data

if( have_rows('selection') ):
// loop through the rows of data
while ( have_rows('selection') ) :
the_row();

if( get_row_layout() == 'selectionselection' ):
?>
                            <div class="titre-soustitre">
                                <div class="menu-content" data-id="id-<?php  the_sub_field('id'); ?>">
                                    <p class="demo bis"><span class="sub">&nbsp;</span></p>
                                    <a href="#" class="expander"><h1><p class="demo title"><?php  the_sub_field('title'); ?></p></h1></a>              
                                    <p class="demo bis"><span class="sub"><?php  the_sub_field('subhead'); ?></span></p>
                                </div>
                            </div>
                <?php 
endif;
endwhile; else :
// no layouts found
endif;
?>
        </div>
        <?php  wp_reset_postdata();// IMPORTANT - reset the $post object so the rest of the page works correctly  ?>
        <?php  endif; ?>
Run Code Online (Sandbox Code Playgroud)

更新2:为了帮助您理解:这是另一页的行,我正在通过$ post_object抓取

                <?php
                // check if the flexible content field has rows of data
                if( have_rows('selection') ):
                // loop through the rows of data
                while ( have_rows('selection') ) : the_row();
                if( get_row_layout() == 'selectionselection' ):?>





                            <div class="titre-soustitre">


                                <div class="menu-content" data-id="id-<?php the_sub_field('id');?>">


                                    <p class="demo bis"><span class="sub">&nbsp;</span></p>
                                    <a href="#" class="expander"><h1><p class="demo title"><?php the_sub_field('title');?></p></h1></a>              
                                    <p class="demo bis"><span class="sub"><?php the_sub_field('subhead');?></span></p>





                                </div>

                            </div>





                <?php endif;
                endwhile;
                else :
                // no layouts found
                endif;

                ?>
Run Code Online (Sandbox Code Playgroud)

Nic*_*dze 4

我认为您需要向主页添加自定义字段。它可以是“帖子/页面”字段(不记得它到底是如何调用的)。这个想法是在主页后端指示您将从哪个页面 id 获取重复器或灵活内容字段的最后一行。

  1. 添加自定义字段以指示主页上的页面 ID。

  2. 现在在主页模板上您需要编写如下内容: $otherPageId = get_field('your_other_page_id');

  3. 然后你可以运行与代码中相同的东西,但是进入

    有_行('选择')

函数添加第二个参数

 have_rows('selection', $otherPageId)
Run Code Online (Sandbox Code Playgroud)

以指示您要在哪个页面上搜索该字段。

  1. 为了获得最后一行,您可以使用多种方法。一种是将行内容分配给数组,然后使用数组的最后一项,或者这里是另一个片段,可以让您了解如何以 ACF 方式执行此操作:

$repeater = get_field('repeater');

$last_row = 结束($repeater);

回声 $last_row['sub_field'];