Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get last row from an ACF flexible content and display it on another page

I made two pages, a front page and an "basic content page".
On this "basic content page", I made a flexible content with different text and images.

I search for a way to display the last row on the front page, is it possible ?

UPDATE : Here is the last code, it can grab the content from another page using "post object field" (named "relation") thanks to @Nick Surmanidze. Only remain the question of how to grab the last row.

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

UPDATE 2 : In order to help you understanding: Here is the ROW of the other page, that I am grabbing through $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;

                ?>
like image 568
Yagayente Avatar asked Jun 09 '16 10:06

Yagayente


1 Answers

I think you would need to add a custom field to home page. It can be a "post / page" field (do not remember how exactly it is called). The idea is to indicate on home page back end which page id are you going to get the last row of repeater or flexible content field from.

  1. Add custom field to indicate the page id on home page.

  2. Now on home page template you need to write something like: $otherPageId = get_field('your_other_page_id');

  3. Then you can run the same thing as you have in your code but into

    have_rows('selection')

function add second parameter

 have_rows('selection', $otherPageId)

in order to indicate which page are you going to search this field on.

  1. For getting the last row you can use many ways. One would be to assign row content to array and then use last item of an array or here is another snippet that can give you an idea how to do it in ACF way:

$repeater = get_field('repeater');

$last_row = end($repeater);

echo $last_row['sub_field'];

like image 110
Nick Surmanidze Avatar answered Nov 15 '22 10:11

Nick Surmanidze