Support

Account

Home Forums General Issues Related posts with custom text

Helping

Related posts with custom text

  • Hello,
    I would like to have some advice on the best way to get a related posts custom field with custom text before each post.

    I’ve managed to create a classic related post with a relationship field (named “rebonds”) where I can select the different posts I want to link at the end of my post using this code :

    <?php 
    
    $posts = get_field('rebonds');
    if( $posts ): ?>
    <div class="titrereb">REBONDS</div><hr />
       <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <span class="symbreb">☰</span>  <a itemprop="url" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                                    <span class="post_author">
                                        <?php esc_html_e('par','bridge'); ?>
                                        <?php
    									if ( function_exists( 'coauthors_posts_links' ) ) {
    										coauthors_posts_links();
    									}
    									else {
    									?>
                                        <a itemprop="author" class="post_author_link" href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php the_author_meta('display_name'); ?></a>
    									<?php } ?>
                                    </span> le <span itemprop="dateCreated" class="date entry_date updated"><?php the_time('d F Y'); ?></span><br />
        <?php endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>	

    So it displays a list of links with author(s) and date.

    What I would like is the option to add, for each relationship, before the link, a custom text depending on the type of post : “Read our interview”, “Read our report”, etc.

    Thanks for your help !

  • 
    setup_postdata($post);
    $post_type = get_post_type($post->ID);
    switch ($post_type) {
      case 'interview':
        echo 'Read our interview';
        break;
      case 'report':
        echo 'read our report':
        break;
      // etc...
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.