Home Forums Front-end Issues Excerpt from relationship


Excerpt from relationship

  • Hi, I’m trying to pull an excerpt from a relationship for featured posts on a static home page. I’m basing it on the code below…

    $posts = get_field('relationship_field_name');
    if( $posts ): ?>
    	<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
    	    	<a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
    	    	<span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
    	<?php endforeach; ?>
    <?php endif; ?>

    Does anyone have any idea how to do this? I’ve looked around and not found anything that really works.


  • Seriously, no help on here? 🙁

  • Same problem, could not get neither excerpt, nor content using relationship. I solved using Basic loop (without setup_postdata), instead of using postdata

  • 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">
    	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,'[&hellip;]');
    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 href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br>
    	            			By <?php the_author(); ?> / <?php the_time( get_option( 'date_format' ) ); ?>
    	            		<?php the_excerpt(); ?>...
    	            	<a href="<?php the_permalink(); ?>" class="read-more">Read more</a>
    	    <?php endforeach; ?>
    	    <?php wp_reset_postdata(); ?>
    	<?php endif; ?>
  • it doesn’t work for me 🙁

  • Can you post your code?

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

The topic ‘Excerpt from relationship’ is closed to new replies.