Home › Forums › Front-end Issues › Display related posts using specific tag via taxonomy custom field › Reply To: Display related posts using specific tag via taxonomy custom field
Thanks James, I had totally forgotten that I had raised the topic here. I have since found the solution and its working perfectly well. Here’s the code I am using for reference:
<div class="post">
<?php
$relatedd = get_field('related');
?>
<?php if( $relatedd ): ?>
<?php foreach( $relatedd as $related ): ?>
<div class="post-image related">
<?php if (has_post_thumbnail($related->ID) ) : ?>
<?php echo get_the_post_thumbnail($related->ID, 'tab-small'); ?>
<?php endif; ?>
</div><!-- .post-image -->
<div class="post-content">
<h3><a href="<?php echo get_permalink( $related->ID ); ?>">
<?php echo get_the_title( $related->ID ); ?>
<?php /* echo wp_trim_words( get_the_title( $related->ID ), 01, '...' ); // trim the words in a title to first word */ ?>
</a></h3>
<?php echo custom_field_excerpt( $related->ID ); ?>
</div><!-- .post-content -->
<?php endforeach; ?>
</div><!-- .post -->
The above code works in the CPT page template and in addition here is the code for the functions.php:
function custom_field_excerpt($related_post_id) {
global $post;
$text = get_field('introduction', $related_post_id );
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = 70; // 20 words
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('the_excerpt', $text);
}
The above code shortens the text from the Introduction field and uses it in related posts.
Hope it is useful for anybody looking for similar solution.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.