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/
Related
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 am having a hard time getting the Wordpress thumbnail to show up when I am using Advanced Custom Fields Pro Post Object.
My goal is to have the user select a single featured post to show up after the 6th post on the blog page.
This is my code (which is pretty much directly from ACF documentation):
<?php
$featured_post = get_field('featured_post', 'option');
if( $featured_post ): ?>
<?php echo esc_html( $featured_post->post_title ); ?></h3>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
This returns the correct title but the wrong featured image (from the post above).
The code above is called inside the loop on index.php inside the featured template part.
$counter = 1;
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
get_template_part( 'template-parts/content', 'get_post_type()' );
//check the counter and display your content
if( $counter === 3 && !is_paged() ) { ?>
<?php get_template_part('template-parts/components/component', 'subscribe'); ?>
<?php } elseif ( $counter === 6 && !is_paged() ) { ?>
<?php get_template_part('template-parts/components/component', 'featured'); ?>
<?php }
//update the counter on every loop
$counter++;
endwhile;
I have tried the other code examples on the documentation page but those return all of my posts (again with only one thumbnail, not the correct thumbnail per post).
I am not sure where to go from here.
Anyone who can help would be greatly appreciated!
Since this is not in the loop context, the function does not find the correct post object set up to take the ID from. Use get_the_post_thumbnail instead, that takes the post id as parameter.
<?php echo get_the_post_thumbnail( $featured_post->ID ); ?>
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
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.