Forum Replies Created

  • 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">
    						$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 */ ?> 
    									<?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.

  • Hello James,

    Sorry about posting after such a gap but had a professional project come up and take all my attention. This being a personal passion had to take backseat 🙂

    I can understand what you are talking about, but I lack the knowledge to convert it into a working function. How do I pass the post_id parameter?

  • Hi James,

    Sorry to bother you but how can I also get thumbnail image to display likewise ? I am uploading a set of images on each page for image slider through a custom field – images_slider. I’ve tried the codes below,but none of them works:

    <?php $image = wp_get_attachment_image_src(get_field('image_test'), 'full'); ?>
    <img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('image_test')) ?>" />
    									$image = get_field('images_slider');
    									$size = 'thumbnail';
    									if( !empty($image) ): ?>
    									<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
    									<?php endif; ?>
    <img src="<?php the_field('images_slider'); ?>" alt="" />
  • Thanks @James, you are a saviour. It worked like a charm!! 🙂

  • Hi @James,

    Thanks, but it still runs the error. I think the problem is that all custom posts have the same custom field named ‘introduction’. When getting to display the related post, it picks up the Title and permalink of the related post correctly, but picks up the ‘introduction’ field of the current post.

    Somehow, while it is able to pick up the title and permalink from ‘related’ custom field, it should also do this for other custom fields of the same post.

  • I tried using this code which I found here :

    							$tagValue = get_field('related_places_by_tag');
    							'tag_id' => $tagValue,
    							'posts_per_page'=>6 // Number of related posts to display
    							$my_query = new wp_query( $args );
    							while( $my_query->have_posts() ) {
    							<a href="<? the_permalink()?>">
    							<?php the_title(); ?>
    							<?php }  

    Though it does not break the template, it doesn’t work.

Viewing 6 posts - 1 through 6 (of 6 total)