Home Forums General Issues Output Custom Fields Relational Post Object Field Reply To: Output Custom Fields Relational Post Object Field

  • Hi John,
    Thank you for the example link. Would I use Display list of posts (without setup_postdata) below?

    I am not sure what exactly is setup_postdata?

    $featured_posts = get_field('featured_posts');
    if( $featured_posts ): ?>
        <?php foreach( $featured_posts as $featured_post ): 
            $permalink = get_permalink( $featured_post->ID );
            $title = get_the_title( $featured_post->ID );
            $custom_field = get_field( 'field_name', $featured_post->ID );
                <a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
                <span>A custom field from this post: <?php echo esc_html( $custom_field ); ?></span>
        <?php endforeach; ?>
    <?php endif; ?>

    Since my Custom field is related_posts would I just change the below?

    $custom_field = get_field( 'field_name', $featured_post->ID );

    Change to…

    $custom_field = get_field( 'related_posts', $featured_post->ID );

    How can I add this all as a shortcode? I use code snippets plugin so I can create a snippet to add the whole. How can I put text before ” Related Posts:” What if there are no related posts will it return nothing?