Support

Account

Home Forums Add-ons Repeater Field Options Page Repeater Row of Current Page Only

Solved

Options Page Repeater Row of Current Page Only

  • Hello,

    I’ve been tearing my hair out over this issue for hours 🙁

    I have an options page that has a repeater with a colour picker and post object (allows multiple to be selected). The idea is that from the options page the user can set global colour schemes to multiple pages.

    I’ve set up a dynamic CSS file for the relevant classes (basically just using the hex value as a class) which is working fine but what I also need to do is add a CSS class to the entry-content tag of the relevant pages with the hex value class to target the relevant blocks and elements that will change colour.

    Here’s what I’ve got so far:

    <?php 
    if( have_rows('acf-colours', 'option') ):
     while( have_rows('acf-colours', 'option') ) : the_row();
     $post_objects = get_sub_field('pages');
      if( ($post_objects) ):
      ?>
       sub-menu-<?php the_sub_field( 'colour' ); ?>
    <?php
      endif;
     endwhile;
    endif;
    ?>

    The above code will list all ‘pages’ sub fields but I’ve tried dozens of iterations of the above code and I can output all the pages repeaters, but I only want to output the repeater that has the current page ID in it.

    Any help greatly appreciated.

    Thank you

  • 
    <?php 
    $current_post = $post_id;
    if( have_rows('acf-colours', 'option') ):
     while( have_rows('acf-colours', 'option') ) : the_row();
     $post_objects = get_sub_field('pages');
      if( ($post_objects) ):
        foreach ($post_objects as $post_object) {
          if ($post_object->ID == $current_post) {
            ?>
              sub-menu-<?php the_sub_field( 'colour' ); ?>
            <?php
          } // end if current post
        } // end foreach post
      endif;
     endwhile;
    endif;
    ?>
    
  • Hi John,

    Thanks for your help, just needed to change $post_id to get_the_ID() in your example and it worked! Yaay.

    <?php 
    $current_post = get_the_ID();
    if( have_rows('acf-colours', 'option') ):
     while( have_rows('acf-colours', 'option') ) : the_row();
     $post_objects = get_sub_field('pages');
      if( ($post_objects) ):
        foreach ($post_objects as $post_object) {
          if ($post_object == $current_post) {
     ?> sub-menu-<?php the_sub_field( 'colour' );
    	  } // end if current post
        } // end foreach post
      endif;
     endwhile;
    endif;
    ?>

    Thanks again for your help.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Options Page Repeater Row of Current Page Only’ is closed to new replies.