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"> </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"> </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;
?>
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.
Add custom field to indicate the page id on home page.
Now on home page template you need to write something like:
$otherPageId = get_field(‘your_other_page_id’);
Then you can run the same thing as you have in your code but into
have_rows(‘selection’)
function add second parameter
in order to indicate which page are you going to search this field on.
$repeater = get_field(‘repeater’);
$last_row = end($repeater);
echo $last_row[‘sub_field’];