ACF: load repeater field from a relationship field - php

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

Related

output the_field of related posts with ACF Relationship

and first off all, thanks for help.
I have to following code to display related posts from one custom post type to another with ACF Relationships.
what i want to know, is it possible and how can i rewrite the code, to output any custom field of the related post that i have selected with the relationship field?
<?php
$posts = get_field('product_id');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $p ): ?>
<li>
<?php echo get_the_title( $p->ID ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
like i do here, is:
echo get_permalink( $p->ID );
i want to echo:
the_field('field_name')
regards,
Axel
If you check the documentation of the the_field() function you'll notice that it can take the post/page ID as the second parameter so you can retrieve the field value of a specific post/page field:
Parameters
the_field($selector, [$post_id], [$format_value]);
$selector (string) (Required) The field name or field key.
$post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post.
$format_value (bool) (Optional) Whether to apply formatting logic. Defaults to true.
So, for example:
<?php
$posts = get_field('product_id');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $p ): ?>
<li>
<?php echo get_the_title( $p->ID ); ?>
<?php the_field('field_name', $p->ID); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

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 Archive Loop - Displaying category or custom post type name, depending on post

I've made a custom loop which displays all posts and custom post type posts on one page.
I am looking to show under the title either the category of the post, or the post type "Case Study", depending on which section the post belongs to. There are no categories in my custom post type "Case Study".
The way the code is written here, it will show the category of the post and nothing if the post belongs to the custom post type "Case Study". I am having trouble setting up the conditional statement to show either a category name or the post type name.
<?php $all_query = new WP_Query(array('post_type'=>array( 'post', 'case-study'),
'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
<?php if ( $all_query->have_posts() ) : ?>
<?php while ( $all_query->have_posts() ) : $all_query->the_post(); ?>
<?php the_title()?>
<?php the_category(', '); ?>
<?php the_excerpt();?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
**EDIT
I have added this code (to replace the_category), but I realize it is sloppy and there is probably a better way. Any input or pointers is greatly appreciated!
<?php if (in_category('blog')) { ?>
<?php the_category(', '); ?>
<?php } elseif (in_category('pr')){ ?>
<?php the_category(', '); ?>
<?php } else { ?>
Case Study
<?php } ; ?>
What about this? I think get_post_type() will use the current post in the loop. If not you need to pass either the ID or the WP_Post object into that function.
if( get_post_type() == 'post'){
the_category(', ');
}elseif( get_post_type() == 'case-study'){
//your code
}

Using post object inside ACF repeater field

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>

Categories