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;
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 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.
I have a list of posts from a custom post type. What I want to do, is for the user to select (checkboxes) all the posts they want to add and create a repeater row for each selection.
Within the repeater, I am using a Post Object field - this post object field is the same as the checkboxes of custom post type posts. (I am doing this, as I need to pull information from each post and the user can then assign further options to this row)
So, how would I allow the user to select these posts and then upon clicking a button, populate the repeater rows? The select value is the post object value from this repeater...
Any help would be appreciated, as I have spent far too long on this, with no solution in sight!
Create a new field group and link it with the post type you require.
In this field group create a field called posts and make it a relationship type field, filtered with the post type you want to be selectable to create repeater field rows.
Make sure return format is set to Post ID.
Then create a repeater field called posts_repeater with sub rows post (post object field) and quantity (number field).
Make sure the post (post object field) return format is also set to Post ID
Now add this to your functions. (read my code comments)
// run modifications when saving or updating the post
add_action('acf/save_post', 'acf_save_post_type', 20);
/**
* action to run modifications when saving or updating the post
* #param int $post_id
* #return void
*/
function acf_save_post_type($post_id) {
// get our current global post object
global $post;
// check we are on the correct post type else return now
if($post->post_type <> 'post') return;
// if post post status is publish or draft
if($post->post_status == 'publish' || $post->post_status == 'draft') {
// if posts repeater does not already have any rows set
if (!get_field('posts_repeater', $post_id)) {
// get the posts relationship field
$posts = get_field('posts', $post_id);
// create an empty repeater field array
$repeater = [];
// for each post selected in posts relationship field
foreach ($posts as $key => $value) {
// add repeater row and assign current posts field id to post object field
$repeater[] = [
'post' => $value
];
}
// temp remove the save post action
remove_action('acf/save_post', 'acf_save_post_type', 20);
// create the repeater field rows
update_field('posts_repeater', $repeater, $post_id);
// re add the save post action
add_action('acf/save_post', 'acf_save_post_type', 20);
}
}
}
This will then populate the repeater with the posts selected in the posts field when updating the post in the admin.
But if the posts_repeater field already has rows, then nothing with happen.
So in your post editor, if you select some posts in the posts acf relationship field...
Then click update, you will now notice the posts_repeater field has the select posts from the posts field defined in each repeater row. Ready to populate quantities.
If you update the post again, because the repeater field now has row data, nothing will happen.
User will have to manually add any extra posts via the repeater field.
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
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/