I made two pages :
1 – a front page
2 – “basic content page” with a flexible content (rows “selection”) with different text and images.
I use “post object field” in order to get the content from page 2 to page 1.
So now, all the content from the flexible content of the second page are displaying on the front page.
My question is : how could I only display the last row of my flexible content on the first page ?
<?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; ?>
If I’m understanding you correctly, I would do this:
$post_object = array_reverse(get_field('relation'));
$post_object[0]
will now be the last item in that array, so you can then
<div class="titre-soustitre">
<div class="menu-content" data-id="id-<?php echo $post_object[0]['id']; ?>">
<p class="demo bis"><span class="sub"> </span></p>
<a href="#" class="expander"><h1><p class="demo title"><?php echo $post_object[0]['title'] ?></p></h1></a>
<p class="demo bis"><span class="sub"><?php echo $post_object[0]['subhead']; ?></span></p>
</div>
</div>
Hi, thanks for your help 🙂
So I did that, but doesn’t seem to work yet.
<?php
$post_object = array_reverse(get_field('relation'));
$post_object[0]
$repeater = get_field('repeater', $post_object->id);
$last_row = end($repeater);
$last_row['subhead'];
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 echo $post_object[0]['id']; ?>">
<p class="demo bis"><span class="sub"> </span></p>
<a href="#" class="expander"><h1><p class="demo title"><?php echo $post_object[0]['title'] ?></p></h1></a>
<p class="demo bis"><span class="sub"><?php echo $post_object[0]['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; ?>
I guess I should define the ID on the original flexible content ?
So far, the ID I use on the original flexible content are written by hand through a text field like this data-id="id-<?php the_sub_field('id');?>
Should I change that ?
Hi @simtwo
I believe you can check the total rows by using the count() function and then check if the current row is the last row by using the get_row_index() function. Maybe something like this:
$total_rows = count(get_field( 'selection' ));
// 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_index() == $total_rows ) {
echo "show the last row here";
}
endwhile;
else :
// else
endif;
I hope this helps 🙂
The topic ‘Get last row from a flexible content and display it on another page’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.