Using post object inside ACF repeater field - php

I'm using Advanced Custom Fields on my website.
I have a repeater field called anime_par, with sub_field called animateur. Sub field animateur is a post-object.
I’m using this inside a loop in my page, a loop that displays posts from a category inside a custom post type.
What I’m trying to do is to display the post name and post link of the animateur selection inside my page.
Here is the code I’m using but it’s not working, it displays the permalink of my current page, not the one selected in the custom field.
<?php while(has_sub_field('anime_par')): ?>
<?php echo get_title('the_sub_field("animateur")'); ?>
<?php endwhile; ?>
Any suggestions to make this work?
thanks for your help,

This method is working for me, per the repeater and post object docs on ACF. You've got to set up the post object inside of the repeater loop.
I added in your field names, and some completely optional html to show the structure.
Hope it helps.
<!-- Start Repeater -->
<?php if( have_rows('anime_par')): // check for repeater fields ?>
<div class="a-container">
<?php while ( have_rows('anime_par')) : the_row(); // loop through the repeater fields ?>
<?php // set up post object
$post_object = get_sub_field('animateur');
if( $post_object ) :
$post = $post_object;
setup_postdata($post);
?>
<article class="your-post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php // whatever post stuff you want goes here ?>
</article>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<!-- End Repeater -->
<?php endif; ?>

the_sub_field doesn't work without has_sub_field
What you have to do it is use loop with has_sub_field as it said in the documenration http://www.advancedcustomfields.com/resources/functions/the_sub_field/
or you can use get_field('repeater_sluf') like that
$rows = get_field('repeater_field_name' ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['sub_field_name' ]; // get the sub field value

<?php if(get_field('favourite_design_quarters', 'user_'.$current_user->ID)): ?>
<?php while(has_sub_field('favourite_design_quarters', 'user_'.$current_user->ID)):
$company = get_sub_field('company_name');
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $company->ID ), 'package-thumbnail' );
?>
<tr>
<td><img src="<?php echo $image[0]; ?>" alt="<?=$company->post_title;?>" /></td>
<td><?=$company->ID;?></td>
<td style="text-align:left;"><?=$company->post_content;?></td>
<td><?=$company->post_date;?></td>
<td>Delete</td>
</tr>

Related

How to get ACF Post Object to only show 1 post?

I have an ACF Post Object field where a user can select 1 post. I am following the instructions here - https://www.advancedcustomfields.com/resources/post-object/
I want to use setup_postdata so I can use Wordpress template functions, so I am using this code -
<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?>
<?php endif; ?>
and in my 'featured_posts' field on that page I have 1 article selected.
But when I use that code it shows all the posts, not just the single one that is selected.
How can I get it to only show the article that is selected?
Instead of use foreach, declare the variable of the field like $post.
Like this:
<?php
$case1 = get_field('case1');
if( $case1 ): ?>
<?php
$post = $case1;
setup_postdata($post); ?>
<li>...

ACF - page selector get custom field as well

I am using Advanced Custom Fields and have chosen 'Page Selector' that allows me to choose pages.
On the front end, I am able to code it so that the pages I've chosen are on the home page with the title and page link but I'm also trying to pull through a custom field in that page as well.
Here is that code I have so far;
<?php if( have_rows('page_selector') ):
$i=1;
$count = (count($my_fields['value']));
?>
<?php while( have_rows('page_selector') ): the_row();
// vars
$page = get_sub_field('page');
?>
<div class="lg-col-6 md-col-6">
<div class="sub_service">
<?php
// vars
$post_id = get_sub_field('page', false, false);
// check
if( $post_id ): ?>
<h2><?php echo get_the_title($post_id); ?></h2>
<?php endif; ?>
//This is the custom field
<?php get_sub_field('description'); ?>
</div>
</div>
<?php
$i++;
endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Apologies in advance if it doesn't make sense, will try and explain further if it's confusing.
Thanks
You post the question, then the answer comes to you...
<?php the_field('description', $post_id); ?>

Wordpress ACF repeater field loop

I am trying to loop out a repeater field in my wordpress front-page template
But for some reason the div is empty and doesn't seem to work.
I am 100% sure my code is correct so there must be an issue.
Anyone has any idea what it could be?
This is how my loop looks like, the field keys are correct! :)
<?php
/**
* Template Name: Front Page Template
*/
?>
<?php while(have_posts()) : the_post(); ?>
<?php if( have_rows('achtergrond_afbeeldingen') ): ?>
<div class="slider-circles">
<?php while ( have_rows('achtergrond_afbeeldingen') ) : the_row(); ?>
<p id="slide1" data-bgimage="<?php the_sub_field('image'); ?>" class="transparent-circle slick-active"></p>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
This is what my front-page.php looks like. The funny thing I've used this in an other project before and everything worked fine. Now my screen is just blank, I have no idea what's going on.
if have_rows('field_56e7d8bebb545') is really true, debug the_row();
var_dump(the_row());
You can view the_result, if is empty, maybe you must set a second parameter for have_row($field_name, $post_id)
field_56e7d8bebb545 is the field key, not the field name. The field name must be used in the have_rows() function. This can be found on the Advanced Custom Fields screen next to the label -
Try this one .. since you have not printed any such thing that can be displayed in browser .. you only provided the data attribute to div.
Write something inside inner HTML might see you repeater values
<?php
/**
* Template Name: Front Page Template
*/
?>
<?php while(have_posts()) : the_post(); ?>
<?php if( have_rows('achtergrond_afbeeldingen') ): ?>
<div class="slider-circles">
<?php while ( have_rows('achtergrond_afbeeldingen') ) : the_row(); ?>
<p id="slide1" data-bgimage="<?php the_sub_field('image'); ?>" class="transparent-circle slick-active"><?php the_sub_field('image'); ?></p>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>

ACF: load repeater field from a relationship field

I trying to load a repeater field from related posts with advanced custom field.
I have a post who has a relationship to load posts from another post_type. Then this post_type has a repeater_field. I tried to load these repeater_fields of my related posts. Its a object inside another object.
Here is my code:
<?php
$posts = get_field('related_posts'); // this is a relation field
if( $posts ):
foreach( $posts as $p ):
?>
<section class="slider">
<?php
$quotes = get_field('slider_quotes'); // this is my repeater field
if( have_rows($quotes) ):
while ( have_rows($quotes) ) : the_row();
?>
<div><h2><?php echo get_sub_field('quote'); ?></h2></div>
<?php endwhile; else:
echo "Nothing yet";
endif; ?>
</section>
I already tried:
$frases = get_field('slider_quotes', $p->ID);
and
<?php echo get_sub_field('quotes', $p->ID); ?>
And I got nothing.
Thanks!
FULL CODE
https://gist.github.com/pailoro/1541717925d9cd9622ba
If 'slider_quotes' is an repeater field, try to get with
<?php the_repeater_field('slider_quotes', $p->ID); ?>
refer the link to get values of repeater field

Post Objects within repeater field (WordPress ACF)

I'm working with ACF in WordPress.
I have a custom post type called Projects. Within there the user has the option to upload 2 featured images through an ACF repeater field.
Now, on the home page I've given the user the option to select 8 Post Object's from the Projects post type.
I need to be able to loop through this home page repeater field, and pull out both featured images and the project title from each 'project' post object.
ACF has recently depreciated the repeater_field function which I think it throwing me off here.
But, here's what I've been trying to work with so far:
<!-- check for repeater field -->
<?php if(get_field('featured-projects')): ?>
<?php while(has_sub_field('featured-projects')): ?>
<!-- get project post objects -->
<?php $projects = get_sub_field('project'); ?>
<!-- without the loop below, this echo's all 8 projects ID's -->
<?php echo($projects->ID); ?><br />
<!-- when added, only pulls the first project. And limits the echo above to the first ID -->
<?php $loop = new WP_Query( array(
'post_type' => 'projects',
'p' => $projects->ID
) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endwhile; ?>
<?php endif; ?>
I've tried to comment the code, but if anything does't make sense, let me know.
Here is how i'd do it, though I realize its a departure from your method. I've included explanation in the code comments:
<?php $featured_projects = get_field('featured-projects'); //Set $featured_projects to equal the array of projects from the home page repeater. ?>
<!-- check for repeater field -->
<?php if($featured_projects): ?>
<?php foreach($featured_projects as $featured_project) : //Loop through each featured project ?>
<?php $project_id = $featured_project['project']->ID; //Get the id for the current featured project ?>
<?php $project_title = get_the_title($project_id); //set $title to be the title of the project ?>
<?php project_featured_images = get_field('name-of-featured-repeater-field-here', $project_id); //get the repeater field of the featured images from the project post ?>
<h1 class='title'><?php echo $project_title; //print the title ?></h1>
<?php if($project_featured_images[0]): //check if you have a 1st image (size large) ?>
<img class='featured-image-one' src="<?php echo $project_featured_images[0]['name-of-the-featured-image-sub-field-here']['sizes']['large']; //print the url to the 1st image; ?>"/>
<?php endif; ?>
<?php if($project_featured_images[1]): //check if you have a 2nd image ?>
<img class='featured-image-two' src="<?php echo $project_featured_images[1]['name-of-the-featured-image-sub-field-here']['sizes']['large']; //print the url to the 2nd image (size large); ?>"/>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
Make sure to fill in the name of your project featured image repeater field, and the name of the image subfield within that repeater. This is clearly a more standard PHP based solution than the API version. I typically use this method per Elliot Candon's (ACF Developer) recommendation.
You can also get different image sizes of the featured images by changing the 'large' to another standard size, or by adding a custom size.

Categories