I am using relationship field inside Flexible content. There is only options to get the WP_Post Object values like the_title()
,the_content()
but the custom field value like the_sub_field('title')
, the_sub_field('background_image')
are not available. Check my code below.
<?php if( get_field('content_blocks', 24) ): ?>
<?php while( has_sub_field('content_blocks', 24) ): ?>
<?php
$posts = get_sub_field('related_practice');
if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<div class="box" style="background-image: url(<?php the_sub_field('background_image');?>)">
<div class="mask">
<div class="content">
<div class="title"><?php the_title(); ?></div>
<div class="description"><?php the_sub_field('title');?></div>
<a href="<?php echo get_page_link( $page->ID ); ?>" class="more">Learn Name</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif;?>
Thanks,
Chandru.
See my comments inside the code and answer the question
I'm assuming that we are referring to the main post here
I will refer to this as post #1
'content_blocks' is on post #1
<?php if( get_field('content_blocks', 24) ): ?>
<?php while( has_sub_field('content_blocks', 24) ): ?>
<?php
$posts = get_sub_field('related_practice');
if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
The code is not looking at a related post
I will call this post #2
All calls to all functions inside the loop
refer to post #2
if you are trying to get values related to
post #1 they will not work
<div class="box" style="background-image: url(<?php the_sub_field('background_image');?>)">
<div class="mask">
<div class="content">
<div class="title"><?php the_title(); ?></div>
<div class="description"><?php the_sub_field('title');?></div>
<a href="<?php echo get_page_link( $page->ID ); ?>" class="more">Learn Name</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif;?>
is ‘background_image’ from post #1 or #2
are you trying to get the title from post #1 or #2
the the sub field ‘title’ from post #1 or #2
are you trying to get the page link from post #1 or post #2
I’m only guessing, but your question leads me to believe that you’re trying to get information for post #1 inside the post #2 loop and that’s the reason it’s failing?
It that’s not the case then let me know. If it is the case then you’re going to need to rework these loops so that you’re getting the data you want at the right time.
We are trying to get the information from post #2 only.
(‘background image’, ‘title’, ‘sub field title’, ‘page link’)
For the post #2, only WP_Post Object details like the_title() and the_content() are available but not custom field values.
Thanks,
Chandru
Your answer doesn’t make any sense. Did you mean #1 for the first group or the second.
Hi,
Sorry for the confusion.
Actually post #1 has a relationship field inside Flexible content. In the relationship field, I selected post #2.
There is no way to get the information for the post #2 custom fields. Only post details are available like the_title() and the_content().
I’m still not sure what you’re trying to get where, your explanation did not clear up the confusion for me. My only guess is that you’re trying to get data from a post that is not the “CURRENT” post. This is the only thing that I can think of that would cause the problem you’re having. If this is not the case then you’re going to need to be more clear about what you’re trying to get where.
You can overcome this by specifying the post ID for the post you want to get the data from and using functions that allow you to specify the post ID.
For example: get_field('field_name', $post_id);
, get_the_title($post_id);
, etc.
The topic ‘Flexible content – relationship – custom field value not getting’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.