Home › Forums › Add-ons › Repeater Field › Using array_unique with repeater fields to remove duplicate @font-face requests
I’m using PHP to parse the below code into a CSS file which is working perfectly, but I’m hoping to remove any duplicates as and when they appear in order to cut down on unnecessary code:
if( have_rows('global_typography', 'option') ):
while( have_rows('global_typography', 'option') ) : the_row();
if( get_sub_field('global_custom_font_file') ):
print '@font-face {' . PHP_EOL;
print 'font-family: '; the_sub_field('global_custom_font_family_name'); print ';' . PHP_EOL;
print 'src: url('; the_sub_field('global_custom_font_file'); print ');' . PHP_EOL;
print 'font-weight: normal;' . PHP_EOL;
print '}' . PHP_EOL . PHP_EOL;
endif;
endwhile;
endif;
I found this article which seems to cover what I’m after, but I’m really struggling to get this working. Here’s my interpretation of this code based on the original code above:
if (have_rows('global_typography', 'option')):
while (have_rows('global_typography', 'option')) : the_row();
$custom_font_file = get_sub_field('global_custom_font_family_name');
$results = array_unique($custom_font_file);
foreach ($results as $result)
print '@font-face {' . PHP_EOL;
print 'font-family: '; the_sub_field('global_custom_font_family_name'); print ';' . PHP_EOL;
print 'src: url('; the_sub_field('global_custom_font_file'); print ');' . PHP_EOL;
print 'font-weight: normal;' . PHP_EOL;
print '}' . PHP_EOL . PHP_EOL;
endwhile;
endif;
Here’s also a screenshot of the ACF field group in question:
Is anybody able to offer any advice or point me in the right direction? Your help is much appreciated!
I managed to solve this myself. According to this post, it looks as though array_unique doesn’t work with sub_field text strings on ACF. Instead, using an if !in_array command worked fine, as seen in the below example:
if( have_rows('global_typography', 'option') ):
while( have_rows('global_typography', 'option') ) : the_row();
// display your sub fields
$custom_font_file = get_sub_field('global_custom_font_file');
// compare current value in saved array
if( !in_array( $custom_font_file, $PreValue ) )
{
print '@font-face {' . PHP_EOL;
print 'font-family: '; the_sub_field('global_custom_font_family_name'); print ';' . PHP_EOL;
print "src: url('"; the_sub_field('global_custom_font_file'); print "');" . PHP_EOL;
print 'font-weight: normal;' . PHP_EOL;
print '}' . PHP_EOL . PHP_EOL;
}
// save value in array
$PreValue[] = $custom_font_file;
endwhile;
endif;
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.