I want to build a function that will echo/display the highest (max) value from two different arrays (repeater fields). The difficulty of the task lies into a specific construction of my repeaters / fields which looks as follows:
+Main repeater (called 'sales')
++sub_field('all_regions_percentage_off')
++Nested repeater (called 'different_regions')
+++ sub_field('percentage_off')
All of the subfields are text one, in which a user inputs numbers. So at the end I would like to display a single value which would the highest among those two fields/arrays. I have the following code:
<?php
$groupA = array();
$groupB = array();
if( have_rows('sales') ):
while( have_rows('sales') ) : the_row();
$groupA[] = get_sub_field('all_regions_percentage_off');
if( have_rows('different_regions') ):
while( have_rows('different_regions') ) : the_row();
$groupB = array_push($groupA, get_sub_field('percentage_off'));
endwhile;
endif;
echo '<pre>'.print_r($groupB).'</pre>';
endwhile;
else : echo 'Nothing here, sorry';
endif;
?>
But it doesn't work and I have no idea why. Plus if the repeater has more than 1 row, the loop displays multiple outputs. Obviously it might be a thing of adding an counter like:
$i = 0; // before first while of the main repeater
if(!$i++) { code here } // as a wrapping element for the first subfield and the entire nested repeater
But again the main issue is in a different place = showing a single value that will be a MAX from both fields (all_regions_percentage_off and percentage_off).
Thanks for any tips and suggestions.
Related
I'm trying to show the ACF repeater image in the dynamic slider in oxygen via the PHP function from the specified page id.
ACF field: slajder
Subfield with img repeater: obraz_slajdera
Page id: 7219
I always get background-img unknown.
My code:
function get_slider() {
$image = get_field( 'img', 7219 )['sizes']['large'];
return $image;
}
Please help.
You may want to do some reading through the ACF developer documentation, your code is fragmented, and the get_field() function isn't being used properly. At any rate, I'll do my best to explain what you should be directing your solution towards. You'll first need to loop through your repeater fields, check to see if there's any "rows" in the repeater field, fetch your value, and then do whatever you need to do with your respective image.
So, technically your function should look something like this:
function fetchSliderImages() {
//Empty array for holding your slider images, assuming there's more than one
$sliderImages = array();
//Check to see if the slider even has rows
if( have_rows('slajder') ):
//Loop through all the rows of your slider
while( have_rows('slajder') ) : the_row();
//Assuming this is coming back as a URL (can be adjusted within ACF)
$imageRepeaterValue = get_sub_field('obraz_slajdera');
//Check to see that we actually got something back that isn't blank
if($imageRepeaterValue != '') {
array_push($sliderImages, $imageRepeaterValue);
}
endwhile;
endif;
return $sliderImages;
}
You can then use this function to return an array of URL's which are slider images.
Used this for reference: https://www.advancedcustomfields.com/resources/repeater/
I'm doing some displaying of tables and my table field is in a repeater field since i want to display 4 tables after each other that is a part of each other.
I'm looking at the code and see that it returns an array of 4.
I have looked at this code: https://wordpress.org/plugins/advanced-custom-fields-table-field/#screenshots
But that only seem to work if the table is in a normal field and not inside a repeater field. I can't get any information out of it.
I have been trying to loop trough the repeater field and then run the code for the table, but that does not seem to work, all I get from the dump is NULL.
if( have_rows($table) ): // loop through the rows of data
while ( have_rows($table) ) : the_row();
var_dump($table['information_table'])
endwhile;
else :
// no rows found
endif;
Anyone that has any tips on how i can get the tables to show from a repeater field?
Thanks.
repeater tables
The plugin author provides this code example:
Not sure what you are missing. Try without the $table variable and use just the field name. Verify that you are using the correct sub_field name. Double check that you have the data saved in your database. It looks like you should be using sub_field('information_table') instead of $table['information_table']
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// get a sub field value (table field)
$table = sub_field('sub_field_name');
// use the “Output Table HTML” code with $table
endwhile;
else :
// no rows found
endif;
I'm fairly new in using ACF so I used the code that was displayed in their site to display what is inside of my repeater field.
However, when I try to show the repeater's content it is empty??
This is my code, this is still in trial mode just to see if it's working-which it isn't. My repeater field already has 2 rows but it's not showing up any of those and just displays the else:
// check if the repeater field has rows of data
if( have_rows('map_infogrp') ):
// loop through the rows of data
while ( have_rows('map_infogrp') ) : the_row();
// display a sub field value
the_sub_field('google_map');
the_sub_field('branch_name');
//$street = get_sub_field('street');
//$district = get_sub_field('district');
//$phonenum = get_sub_field('phone_number');
//$email = get_sub_field('email');
endwhile;
else:
echo 'why is this empty???';
endif;
You need to specify the page id that you have set the ACF Repeater, otherwise it will get from the current page ID
have_rows($field_name, $post_id); //syntax
So, update your loop inserting the page ID you've entered the repeater data:
if( have_rows('map_infogrp', 'page_id') ):
// loop through the rows of data
while ( have_rows('map_infogrp', 'page_id') ) : the_row();
...
If you have the fields filled in on the specific page it should be showing up. If not, double check the field name(not label) that you used. If you have more than one row make sure you're printing it out as an array too
Im using ACF pro and for a search loop I need to get all fields of a page or post including subfields (repeaterfield) into an array for processing, so i can extract data like "name" and "value" from the array-objects.
I was trying "get_field_objects(mypageid);" which seems neat, but it only gets the first level fields. I need to get the subfields too.
I was looking around and right now it seems there isnt anything out of the box for it with ACF, but what would be the best way to get this data? Especially since every subfield could have subfields themself. I fail to wrap my head around a loop function for that. Has anyone ever written a loop for exactly that?
Any help would be much appreciated!
Thanks in advance,
ANB_Seth
How to Get ACF Repeater Field?
add below code in your page or post
Example :
main ACF filed name :homepage_slider
Sub Field name : slider_title, slider_description
<?php
if( have_rows('homepage_slider') ):
// loop through the rows of data
while ( have_rows('homepage_slider') ) : the_row();
?>
<h3><?php the_sub_field('slider_title'); ?></h3>
<p><?php the_sub_field('slider_description'); ?></p>
<?php
endwhile;
else :
// no rows found
endif;
?>
Okay, so I have worked myself to this functons which seem to do the trick:
function abGetAllFields($pro_num){
$tmpArr = get_field_objects($pro_num);
$fillArray = array();
foreach( $tmpArr as $tmpFieldObject ) {
$fillArray = abGetAllFieldsCycle($fillArray, $tmpFieldObject["name"], $tmpFieldObject["value"]);
}
return $fillArray;
}
function abGetAllFieldsCycle($fillArray, $name, $value) {
if (is_array($value)) {
foreach( $value as $key => $value ) {
$fillArray = abGetAllFieldsCycle($fillArray, $key, $value);
}
} else {
array_push($fillArray, [$name, $value]);
}
return $fillArray;
}
$myFieldArray = abGetAllFields($pageId);
There might be a better or easier way to do it, but heck, it did what I needed so I'll stop right there and thought I'd share my result :-)
I was wondering how I can create a dynamic set of IDs based off the amount of custom posts that are queried for the given post.
I'm using the Advance Custom Fields plugin and then I"m querying the custom fields in the given post. If you take a look below you'll see I have my custom fields being queried each one is wrapped in a div with an id "section-1". What I need is for the "section-1" to update to "section-3", "section-4" each time a new field name is queried. So if 5 fields are queried they each have their own ID.
<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
<div id="section-1">
the_sub_field('sub_field_name');
</div>
endwhile;
else :
// no rows found
endif;
?>
Just set an index variable before the loop and increment it at each iteration. Use that inside your id.
<?php
$index = 1;
while ( have_rows('repeater_field_name') ) : the_row(); ?>
<div id="section-<?= $index; ?>">
<?php the_sub_field('sub_field_name'); ?>
</div>
<?php $index++
endwhile; ?>
Try this
<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
$i = 0;
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
<div id="section-<?php echo ++$i; ?>">
the_sub_field('sub_field_name');
</div>
endwhile;
else :
// no rows found
endif;
?>