Unable to get ACF repeater field value - wordpress

I created an ACF repeater field on my options page but I'm unable to retrieve the value using get_field(). How do I retrieve the repeater value?

According to ACF - Get values from an options page, you need to add 'option' as the second parameter in get_field().
See this example:
$variable = get_field('field_name', 'option');

<?php if( have_rows('repeater', 'option') ): ?>
<ul>
<?php while( have_rows('repeater', 'option') ): the_row(); ?>
<li><?php the_sub_field('title'); ?></li>
<?php endwhile; ?>
</ul>
Replace repeater with the name of your field and inside the_sub_field() with what you have included.

To get ACF Repeater field you have to use 'echo get_sub_field();' or 'the_sub_field();'
For ACF option page you can use the following code:
<?php if( have_rows('repeater_field_name', 'option') ):
while ( have_rows('repeater_field_name', 'option') ) : the_row();
the_sub_field('yourfieldname');
endwhile;
endif; ?>

Related

How to display image from custom post type in a page with a post object field?

I have two ACF fields, one for custom post type (image field) and the other one (Post Object field) for a page. See https://share.getcloudapp.com/ApuLkGEp
Then I created a plugin using a shortcode to pull those items but I'm not able to get the image from the custom post type to display it in the page.
Below is my code:
if( have_rows('select_team_member') ):
// Loop through rows.
while( have_rows('select_team_member') ) : the_row();
// check for rows (sub repeater)
if( have_rows('set') ): ?>
<div class="staff-feature">
<?php
// loop through rows (sub repeater)
while( have_rows('set') ): the_row();
$member = get_sub_field('member');
$image = get_field_object('field_59081633e8de4', $member->ID);
$size = 'full';
$thumbnail = wp_get_attachment_image( $image, $size );
?>
<div class="set">
<div class="staff-pop-wrap">
<?php var_dump( $thumbnail ); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; //if( get_sub_field('items') ):
// End loop.
endwhile;
endif;
I want to display the bio_portrait image field but it gives me this value: https://share.getcloudapp.com/RBuWO9n5
get_field_object returns an array of settings and the value.
you could use $image['value'] to actually get the value (which might be an object, array or ID according to your return settings).
But you could also just use the normal get_field/get_sub_field methods for images as well..
You will see it, if you were to var_dump($image) for debugging.

ACF Post Object empty return

I have custom post type called "Projects" and inside single-project.php view want to show Posts related to Project.
I have tried to solve that problem by using ACF's Post Object but got nothing from The Loop.
I searched for the solution on Stackoverflow and AFC support page and I could not find a problem.
Maybe this 'Post Object' option is not even created for something like this. I don't know.
Code inside single-project.php
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div>
<h3><?php the_title(); ?></h3>
</div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
ACF configuration
https://i.imgur.com/FNnUbiw.jpg
New post configuration
https://i.imgur.com/IHQqR0P.jpg
Custom post type output
https://i.imgur.com/YFugBZl.jpg
In the place of question marks, I want to show Posts related to that Project.
Since you have allowed multiple values for the field, obtained value will be in array. And since you have set to return value as post object, the value obtained will be the array of objects. After array is obtained, you can loop through those to show related projects.
<div class="related-projects">
<?php
$projects = get_field( 'povezani_projekt' );
global $post;
?>
<?php if ( ! empty( $projects ) ) : ?>
<h3>Related Projects</h3>
<?php foreach ( $projects as $post ) : ?>
<?php setup_postdata( $post ); ?>
<h4><?php the_title(); ?></h4>
<?php wp_reset_postdata(); ?>
<?php endforeach; ?>
<?php endif; ?>
</div><!-- .related-projects -->

Wordpress ACF repeater field > radio button value and babel

I have a repeater field in wich I have a radio field. I need to output the label and the value.
In ACF field, I putted value:label such as :
red: Red Carpet
green: Green leaf
I tried a piece of code :
$field = get_sub_field_object(‘field_name’);
$value = get_sub_field(‘field_name’);
$label = $field[‘choices’][ $value ];
I tried to replace the fieldname by the field_id, but it returns "Array" instead of the value.
I need to use the value in a class, and the label in a title. Can you help me ?
get_sub_field_object() has to be used within a has_sub_field() loop, such as this:
<?php while( has_sub_field('repeater_fields_name') ): ?>
<?php
// vars
$select = get_sub_field_object('radio_field_from_your_code');
$value = get_sub_field('radio_field_from_your_code');
?>
<ul>
<?php foreach( $select['choices'] as $k => $v ): ?>
<li>
<?php if( $k == $value ): ?>
<span class="selected">Selected!</span>
<?php endif; ?>
<?php echo $v; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endwhile; ?>
You're probably close to getting the right values. Just tweak things to follow this general pattern. More info on this function can be found at ACF's docs site: https://www.advancedcustomfields.com/resources/get_sub_field_object/

Display current post after showing posts query result in wordpress

In sigle.php page I need to display a list of post that belong to the category of the post and the I have to display the post content.
So I did a query to get the posts tat belong to the post category:
<?php
$cat = get_the_category();
query_posts('cat='.$cat[0]->cat_ID);
while (have_posts()) : the_post();?>
<li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><?php echo get_the_title();?></li>
<?php endwhile;?>
Now how can I retrive the current post data?
If I do like this
<?php while ( have_posts() ) : the_post(); ?>
<?php echo the_post_thumbnail(get_the_ID());?>
<?php endwhile;?>
I still display query_posts.
I solved using WP_Query instead of query_posts();
<?php
$query = new WP_Query('cat='.$cat[0]->cat_ID);
while ($query->have_posts()) : $query->the_post();?>
<li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><?php echo get_the_title();?></li>
<?php endwhile;?>

Wordpress: How to sort posts on a custom field

I want to display a list of post titles from a custom taxonomy in a particular order.
I thought the best way to control the order would be to add a custom field and sort on that custom field.
The problem I'm having is that I'm trying to use the built-in functionality of Wordpress and I can't find a way to add sort functionality.
My Scenario looks like this
The calling url is ...com/taxonomy/term
This calls up a template, the filename of which is taxonomy-taxonomyname-term.php
My template is simply the index.php template renamed and edited to contain this loop
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
This displays a list of titles but I can't find a way to control the order of the titles.
The only way I've seen to set the order of a group of posts is to define the order of the posts in the query. But of course in this case I dont have a query because I already have the posts via the calling url.
Is there any way to add sort functionality without adding another query or is the query mandatory.
Suppose your custom fields is my_date
You can create custom query like this.
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
To use it
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
For more info http://wpengineer.com/1915/sort-posts-custom-fields/

Resources