Home Forums General Issues Getting first row from repeater field from post object in flexible content


Getting first row from repeater field from post object in flexible content

  • I know the title of my question is a little confusing, so I will first explain what I am trying to do.

    I have a flexbile content field, where I have made a post object field which is multi select. So I can select multiple posts in that field.

    Now the posts which are selected in that field, there is a repeater field in those posts, which has multiple data, like title, description. I want to grab the first row of each repeater field.

    So if I select 2 posts in post object. It will look for those 2 posts and grab the Title, Description from the first row of the repeater field of those 2 posts.

    I hope I have made myself clear ?

    So here is the code I am using to display it in the frontend.

        if( have_rows('customer_landing_page') ):
            while ( have_rows('customer_landing_page') ) : the_row();
                if( get_row_layout() == 'success_stories' ): ?>
                    <div class="inner">
                        <div class="content-wrap">
                            <div class="post-content">
                                <h2>Client Success Stories</h2>
                                <?php $post_objects = get_sub_field('client');
                                if( $post_objects ): ?>
                                    <?php foreach( $post_objects as $post): ?>
                                        <?php setup_postdata($post); ?>
                                            <?php if (get_field('statistics')) : ?>
                                                <?php while(has_sub_field('statistics')): ?>
                                                        <div class="stats-wrap">
                                                            <p class="stats-number"><?php the_sub_field('number'); ?></p>
                                                            <div class="stats-content">
                                                                <p><?php the_sub_field('description'); ?></p>
                                                                <a href="<?php the_permalink(); ?>">Find Out More</a>
                                                                <?php the_post_thumbnail( 'thumbnail', array( 'class' => 'aligncenter' ) ); ?>
                                                <?php break; 
                                    endforeach; ?>
                                    <?php wp_reset_postdata(); ?>
                                <?php endif; ?>
                <?php endif;

    So as you can see, I am first running a while loop for flexbile content, and then using if row layout for that section. Then setting up post data for the post object.

    I am using the break; for the repeater field, so that the loop breaks after first run and advances to the next post.

    However, the problem that I am facing is, that it is outputting only 3 posts, while there are 4 posts, and the other weird thing happening is that it is running into an infinite loop, with the flexible content field repeating itself several times.

    Here is a screenshot of the output so you can understand what’s going on.

    This is just a part of screenshot. This row keeps on repeating till php execution time is exceeded.

    When I use the_title() inside the post object, then it shows those 4 posts that I have selected.

    Thanks !

Viewing 1 post (of 1 total)

The topic ‘Getting first row from repeater field from post object in flexible content’ is closed to new replies.