6

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;

                ?>
Yagayente
  • 341
  • 2
  • 19

1 Answers1

4

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'];

Nick Surmanidze
  • 1,671
  • 1
  • 11
  • 20
  • are you talking about "Relationship" field ? https://www.advancedcustomfields.com/resources/relationship/ – Yagayente Jun 11 '16 at 14:19
  • I was actually talking about post object field https://www.advancedcustomfields.com/resources/post-object/ from which you can retrieve the id or it can be simply an input field and you can input an id. Or you can completely skip it and hadcode the id of "the other page" into the home page template into this function like this have_rows('selection', 99) - assuming that 99 is the id of the other page. – Nick Surmanidze Jun 11 '16 at 14:24
  • Okay thank you. But if I copy and paste the exact same row of content, and change >> have_rows('selection') >> have_rows('selection, 8') , it doesn't seem to work yet. – Yagayente Jun 11 '16 at 14:36
  • have_rows("selection", 9); keep attentionto arguments. Fist is string in quotes and then the second one is just a number without quotes – Nick Surmanidze Jun 11 '16 at 14:40
  • Ok turns out I use "post / page" field as you suggest first, and it worked ! Now I just need to see how to "assign row content to array " as you said, to get the last row, Any idea of how I could do that ? – Yagayente Jun 11 '16 at 14:57
  • :) Can you tell me what it is ? "here is another snippet that can give you an idea how to do it in ACF way:" answer is incomplete – Yagayente Jun 11 '16 at 15:07
  • I have edited answer. Seems like the code I have posted in the end for some reason got cut off and cannot mark it as code. Basically you can use end() function to get the last field of the repeater. – Nick Surmanidze Jun 11 '16 at 15:21
  • should I place those bellow $post_object = get_field('relation'); ? – Yagayente Jun 11 '16 at 15:32
  • if $post_object->id gives you the post id than you have to write something like: $repeater = get_field('repeater', $post_object->id); and then $last_row = end($repeater); and then display the sub fields with: $last_row['sub_field']; – Nick Surmanidze Jun 11 '16 at 15:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114427/discussion-between-simtwo-and-nick-surmanidze). – Yagayente Jun 11 '16 at 15:41