Home › Forums › Front-end Issues › Excerpt from relationship › Reply To: Excerpt from relationship
Hi Gleenk,
You need to add in global $post; and then it works fine. Here’s an example below of what I did for featured posts using the relationship field.
<div class="row home-featured-posts collapse">
<?php
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
remove_filter('the_excerpt', 'wpautop');
function trim_excerpt($text) {
return rtrim($text,'[…]');
}
add_filter('get_the_excerpt', 'trim_excerpt');
global $post;
$posts = get_field('featured_posts');
if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<div class="small-12 medium-4 columns">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('small-letterbox'); ?>
</a>
<div>
<h3>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br>
<small>
By <?php the_author(); ?> / <?php the_time( get_option( 'date_format' ) ); ?>
</small>
</h3>
<p>
<?php the_excerpt(); ?>...
</p>
<a href="<?php the_permalink(); ?>" class="read-more">Read more</a>
</div>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
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.