Home › Forums › Add-ons › Repeater Field › 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:
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 -->
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.