Home › Forums › General Issues › Getting the_content() in a Relationship loop
Hello,
I have created a Relationship field where the user chooses 3 pages which will be featured on the homepage. Each should show the page title, the page link and the content from that page. I used the example basic loop on from this page – http://www.advancedcustomfields.com/resources/field-types/relationship/ – to get started and it works for the most part.
This is what my current code looks like:
<?php $posts = get_field('featured_pages'); ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<div class="col-md-4">
<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
<p><?php the_content(); ?></p>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
It’s getting the_permalink(), it’s getting the_title(), but the_content() doesn’t work. Any ideas?
Hi @cwcarlsen
If the_title
and the_permalink
and working, I don’t understand why the_content
is not working.
Perhaps you can debug the post data like so:
$posts = get_field('featured_pages');
echo '<pre>';
print_r( $posts);
echo '</pre>';
die;
Does each post object contain post_content data?
Maybe the selected posts, don’t contain data? Or perhaps another plugin is preventing the post_content from being displayed such as a member plugin?
Thanks
E
I had this same problem. Then I realised the format was set to Post IDs. As soon as I changed it to Post Objects it worked.
When I use this I get the opposite problem. The content works, but the permalink and title both reference the page they are placed into. When I don’t use the post data, they reference correctly, but the content doesn’t show.
I’m getting the same issue as cwcarlsen.
In my case, I can get the title, permalink, thumbnail and excerpts displaying correctly, but not the_content. Changing the format from Post Objects to Post IDs didn’t work. I’ve been using ACF Pro 5.10 and 5.12.
A quick update to my earlier post – switching to ACF (non-Pro) 4.3.9 seems to have fixed the problem.
I have a loop setup and need to pull in the title, content, and featured image. Everything works except for the_content().
The field is set to post object so I know thats not the issue. Is this something with ACF Pro? I really don’t want to use the old version just because of this. Any help here would be much appreciated.
Version: ACF Pro 5.3.1
Bumping this – have the same issue.
I get the featured image, title and other acf fields, but no excerpt or content.
<h3>Course Faculty</h3>
<?php
global $post;
$faculty = get_field('alt_faculty');
if( $faculty ): ?>
<?php foreach( $faculty as $post): ?>
<?php setup_postdata($post); ?>
<div class="post-author">
<div class="thumb">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>"><img src="<?php echo catch_that_image(); ?>" /></a><?php }?>
</div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
<?php get_template_part( 'partials/profile-social', '' ); ?>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Ah. Fixed the issue by switching the field from post_ID to post object.
Very odd, I’m having the exact same issue but my field is definitely setup as a post object. Anyone else having this issue?
Having this issue too, using the Basic loop (without setup_postdata). Field is 100% set up as Post Object, and I’m able to get the title using:
<?php echo get_the_title( $p->ID ); ?>
but unable to get the content using:
<?php echo get_the_content( $p->ID ); ?>
I was having this issue too using the Basic loop (without setup_postdata) on my blog/news page. I was using a relationship field to select some featured posts for the top of the page but get_the_content() always returned the content of my latest published post.
I over came this using the below code:
<?php $content = get_post_field('post_content', $p->ID); ?>
<p><?php echo $content; ?></p>
Just in case anyone else comes along searching for this, here’s what worked for me:
global $post;
$related_posts = get_field( 'related_posts' );
if( $related_posts ) :
foreach( $related_posts as $post ) : setup_postdata( $post );
// This worked for me
$output = apply_filters( 'the_content', $post->post_content );
echo $post->post_content;
endforeach;
wp_reset_postdata();
endif;
The topic ‘Getting the_content() in a Relationship loop’ 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.