Support

Account

Home Forums General Issues Problem in displaying custom fields from Relationship field

Solved

Problem in displaying custom fields from Relationship field

  • I have a CPT called ‘destination’ within which I have about 100 posts set with custom template with the help of ACF. I wanted to show related posts below each of these posts factored by tags (which I haven’t been able to despite trying several solutions), so I setup a relationship field that lets me select the Related Destination posts manually.

    Now, I’m able to show the title and permalink of the selected post, but not any other fields. This is the code I’m using:

    <?php 
    	$relatedd = get_field('related');
    ?>
    <?php if( $relatedd ): ?>
    	<ul>
    	<?php foreach( $relatedd as $related ): ?>
    	<li>
    	<a href="<?php echo get_permalink( $related->ID ); ?>">
    	<?php echo get_the_title( $related->ID ); ?>
    	</a>
    	<?php echo custom_field_excerpt( $related->ID ); ?>
    	</li>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>

    Specifically, I have a custom field called ‘introduction’ for each post. While showing the related posts, I want to pull the introduction text and shorten it. I am able to use the below code to shorten the ‘introduction’ text with the ‘custom_field_excerpt’ function here:

    `function custom_field_excerpt() {
    global $post;
    $text = get_field(‘introduction’);
    if ( ” != $text ) {
    $text = strip_shortcodes( $text );
    $text = apply_filters(‘the_content’, $text);
    $text = str_replace(‘]]>’, ‘]]>’, $text);
    $excerpt_length = 100; // 20 words
    $excerpt_more = apply_filters(‘excerpt_more’, ‘ ‘ . ‘[…]’);
    $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return apply_filters(‘the_excerpt’, $text);
    }’

    But while using it in the first code to display related posts, it instead displays the current post’s ‘introduction excerpt’.

    Any help would be greatly appreciated!

  • Hi @him

    The issue could be brought about because you are not passing the $post_id parameter to get_field(...) method in your custom_field_excerpt(...) function.

    Try this, change the line where with get field to the following:

    $text = get_field('introduction', $post->ID);

    For more information, have a look at: http://www.advancedcustomfields.com/resources/get_field/

    Hope this helps 🙂

  • 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.

  • Hi @him

    Got you now, thanks for the clarification.

    Well, in that case, you need to pass the post_id of the related post to get_field(...)

    Pass this parameter from <?php echo custom_field_excerpt( $related->ID ); ?> then change custom_field_excerpt(...) method definition to:

    function custom_field_excerpt($related_post_id) {
    	$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 = 100; // 20 words
    		$excerpt_more = apply_filters(‘excerpt_more’, ‘ ‘ . ‘[…]’);
    		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    	}
    
    	return apply_filters(‘the_excerpt’, $text);
    }

    Hopefully, this will work 🙂

  • Thanks @james, you are a saviour. It worked like a charm!! 🙂

  • 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')) ?>" />
    
    *******************************
    
    <?php
    									$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="" />
    
  • Hi @him

    Hmm… Have you tried passing on the $post_id parameter to the ACF functions i.e. get_field() and the_field()?

    The $post_id parameter tells ACF from where to get the field value from. Check out the documentation for more info:
    http://www.advancedcustomfields.com/resources/get_field
    http://www.advancedcustomfields.com/resources/the_field

  • 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?

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

You must be logged in to reply to this topic.