Support

Account

Home Forums General Issues Display title of custom post type within a relationship field

Solved

Display title of custom post type within a relationship field

  • I have a relationship field that displays a featured post, page, custom post type (such as a case study).

    I want to display the title of a custom post type within the relationship field, so it shows whether it’s a case study, article, etc.

    This is my code so far – have got a bit stuck as I can only get it to return ‘page’.

    <?php
                $featured_posts = get_field('select_post_or_page');
                if( $featured_posts ): ?>
    
                        <?php foreach( $featured_posts as $featured_post ):
                            $permalink = get_permalink( $featured_post->ID );
                            $title = get_the_title( $featured_post->ID );
                            $excerpt = get_the_excerpt( $featured_post->ID );
                            $image = get_the_post_thumbnail( $featured_post->ID );
                            ?>
                            <article class="row">
                                <div class="col-6">
                                    <p>Custom post type title here</p>
                                    <h2><a>"><?php echo esc_html( $title ); ?></a></h2>
                                    <p><?php echo $excerpt; ?></p>
                                    <a>" class="btn">Read more</a>
                                </div>
                                <div class="col-6">
                                    <?php echo $image; ?>
                                </div>
                            </article>
                        <?php endforeach; ?>
    
                <?php endif; ?>
        </div>
  • 
    $post_type = get_post_type($featured_post->ID);
    $post_type_object = get_post_type_object($post_type);
    $labels = get_post_type_labels($post_type_object);
    echo $labels->singular_name;
    
  • Perfect, thank you so much!

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

You must be logged in to reply to this topic.