Repeater Field empty in ACF - php

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

Related

Show images in slider from acf repeater

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/

Get max value from two arrays inside two ACF nested repeaters

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.

output table in ACF that is in a repeater field

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;

ACF no rows found using wordpress advanced custom field plugin

hello experts i have been trying to retrieve data from fields that i created
the field name is "ddw" and its repeater and
its subfield op1 and it has lots of rows
but i am still not able retreive any row by using this code
<?php
require_once 'wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
include_once 'wp-content/plugins/acf351/acf.php';
// check if the repeater field has rows of data
if( have_rows(get_field('ddw')) ):
// loop through the rows of data
while ( have_rows(get_field('ddw')) ) : the_row();
// display a sub field value
echo the_sub_field('op1');
endwhile;
else :
echo 'no rows found';
endif;
?>
and it finds no row . i want all rows from every posts and particularly i want http links to put on array and loop through it . i have put this script in wp directory its not theme or template folder. please help me where i am doing wrong .thanks in advance
For your code to work, it should look like this. (replace $post_id with your post id variable)
<?php
require_once 'wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
include_once 'wp-content/plugins/acf351/acf.php';
// check if the repeater field has rows of data
if( have_rows('ddw' , $post_id) ):
// loop through the rows of data
while ( have_rows('ddw', $post_id) ) : the_row();
// display a sub field value
echo get_sub_field('op1');
endwhile;
else :
echo 'no rows found';
endif;
?>
You can find code samples here for all scenarios like without loop or for all posts with post ID.
Try
https://www.advancedcustomfields.com/resources/code-examples/

Advanced Custom Fields for Wordpress

I'm using the advanced custom field plugin on a Wordpress site. I'm using the Repeater Field Type to be able to attach more than one file.
My Repeater Field Type is called: electronics
with the sub fields name: electronics_files
Here is what I have so far:
<?php
// check if the repeater field has rows of data
if( have_rows('electronics ') ):
// loop through the rows of data
while ( have_rows('electronics ') ) : the_row();
// display a sub field value
the_sub_field('electronics_files');
endwhile;
else :
// no rows found
endif;
?>
I have the return value for electronics_files as a File URL so I can wrap it inside of an a tag to download.
Right now it returns all the file urls as one long string. How would I do it so it first checks to see if I have datasheet then grabs the first one wraps it around a a tag then loops until there are no more electronics_files.
I have something like this maybe?
if( $file ) {
$url = wp_get_attachment_url( $file );
?><a href="<?php echo $url; ?>" >Download File</a><?php
}
Basically I just want it to display the file I attached as links that can be downloaded.
It sounds like to just need to change this...
the_sub_field('electronics_files');
...to this:
echo 'Download File';
In other words, just format the sub field as a link.

Categories