Aleš Sýkora / October 2, 2020 / 0 comments

ACF Repeater Post Object in oxygen code block

Post summary: You are using gutenberg and ACF and toolset and need to add related posts in slider, which you choose at the post you are editing? Also you need to put them in the content, exactly where you want? Create ACF repeater with Post Object field insideCreate Oxygen Block for GutenbergAdd code block and FlickityEdit your…

You are using gutenberg and ACF and toolset and need to add related posts in slider, which you choose at the post you are editing? Also you need to put them in the content, exactly where you want?

  1. Create ACF repeater with Post Object field inside
  2. Create Oxygen Block for Gutenberg
  3. Add code block and Flickity
  4. Edit your post and add the Oxygen Block slider.

Code example:

<div class="rep-carousel">
  <?php global $post; ?>
  <?php if (have_rows('vyber_clanku')) : //vyber_clanku is ACF repeater field slug 
  ?>
    <?php while (have_rows('vyber_clanku')) :  the_row(); ?>
      <?php $post_object = get_sub_field('clanek'); //clanek is post object field slug 
      ?>
      <?php if ($post_object) : ?>
        <?php // override $post
        $post = $post_object;
        setup_postdata($post);
        ?>
        <div class="rep-carousell-cell">
          <div class="slider-image"><?php the_post_thumbnail('full'); ?></div>
          <div class="p-1em">
            <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
          </div>
        </div>
        <?php wp_reset_query(); // IMPORTANT - reset the query so the rest of the page works correctly 
        ?>
      <?php endif; ?>
    <?php endwhile; ?>
  <?php endif; ?>
</div>

If this does not work for you, try this:

<?php global $post; ?>
<?php if( have_rows('vyber_clanku') ): ?>
    <?php while( have_rows('vyber_clanku') ): the_row(); ?>
        <?php $post_object = get_sub_field('clanek'); ?>
        <?php if( $post_object ): ?>
            <?php // override $post
            $post = $post_object;
            setup_postdata( $post );
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php wp_reset_query(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
        <?php endif; ?>
    <?php endwhile; ?>
<?php endif; ?>

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts