output the_field of related posts with ACF Relationship - php

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; ?>

Related

Woocommerce / ACF : add class to product <li> depending on ACF Field value

I am trying to automatically add a class to my product <li element in a grid loop, depending on ACF Field value.
I have an ACF Field that is set by the client to decide wether the product background is blue or red. I would need to get the ACF Field color info (set by hex) and add it as a class to the product <li in another page loop.
The functions below could help but I'm not sur how to make them work.
add_filter('post_class', function($classes, $class, $product_id) {
if(is_product_category()) {
//only add these classes if we're on a product category page.
$classes = array_merge(['color-1','color-2'], $classes);
}
return $classes;
},10,3);
and
$fields = get_field_objects();
if( $fields ): ?>
<ul>
<?php foreach( $fields as $field ): ?>
<?php if( $field['value'] ): ?>
<li><?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
endif;
Is there a way to integrate this in my functions.php file and make it work somehow ?
Thanks for your help!

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>...

Nested post object with Advanced Custom Field

I have a movie list to display in a page. Each movie is a post-object (which I repeat with an ACF repeater).
But in these movies, there is another post-object for the authors.
I can not see the name of the author. Can you help me ?
Here is my code
<?php while ( have_rows('sc_movies') ) : the_row(); ?>
<?php $post_object = get_sub_field('sc_movies_movie'); ?>
<?php if($post_object): ?>
<?php $post = $post_object; setup_postdata( $post ); ?>
<article class="movie">
<div class="movie__content">
<h3 class="movie__title"><?= the_title(); ?></h3>
<?php $post_object = get_field('film_author'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; setup_postdata( $post ); ?>
<span class="movie__director">Par <?= the_title() ;?> </span>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</article>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
please make sure if your "sc_movies" repeater has "sc_movies_movie" post object with "Select multiple values? = false " and also in movie post "film_author" post object is "Select multiple values? = false" then your code is correct. If you still gettting issue try add "wp_reset_query()" before while loop because may be some other post object is conflict in page.

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

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