Support

Account

Home Forums Add-ons Repeater Field Display single random repeater row if page matches sub_field [Resolved]

Unread

Display single random repeater row if page matches sub_field [Resolved]

  • I wanted to display a single ad, in the footer of a website, that was relevant to the content of the page it was on. If there were no relevant ads then just display a random ad. This took me a while to figure out (with an assist from ACF Support) and thought I’d share the answer and maybe save someone else some time.

    First I created an OPTION page by adding this code to functions.php:

    /////// ACF Options Page
    
    if( function_exists('acf_add_options_page') ) {
    
        acf_add_options_sub_page('Block Ads');
    
    }

    Then I created a Repeater called “Ad Blocks” with four sub_fields:

    • Link, link, Url
    • Title, title, Text
    • Image, image, Image (image URL)
    • Page ID, page_id, Text

    And in the Repeater Settings > Location Rules I assigned the repeater to the option page.

    Then I went to the Ad Block Option Page and added rows of ads. Note that the page_id is the number of the relevant page.

    Then I added the below “Insert Ad” code to the footer template. It finds the “Ad Blocks” Repeater on the Option Page. Then finds the current page ID and compares it to the page_id subfields. If there are rows that match the current page ID, it grabs one of them randomly and displays those sub_fields. If there are no page ID/page_id matches, it displays a random ad.

    <div class='insert_ad'>
    <?php
    $repeater = get_field( 'ad_blocks', 'option' ); // get the repeater field
    $repeater = array_filter(
            $repeater,
            function( $repeater_row ) {
                    $current_page_id = get_the_ID();
                    if ( $repeater_row['page_id'] == $current_page_id ) {
                            return true;
                    }
                    return false;
            }
    );
    
    if ($repeater == true) {
    $repeater = $repeater[array_rand($repeater, 1)];
    
                        echo '<a class="ad_wrap" href="';
                        echo $repeater['link'];
                        echo '" target="_blank"><img border="0" src="';
                        echo $repeater['image'];
                        echo '" ><p>';
                        echo $repeater['title'];
                        echo '</p></a>';
    } else {
    $repeater2 = get_field( 'ad_blocks', 'option' );
    $repeater2 = $repeater2[array_rand($repeater2, 1)];
    
                        echo '<a class="ad_wrap" href="';
                        echo $repeater2['link'];
                        echo '" target="_blank"><img border="0" src="';
                        echo $repeater2['image'];
                        echo '" ><p>';
                        echo $repeater2['title'];
                        echo '</p></a>';
    }
    ?>
    </div><!-- .insert_ad -->
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.