0
votes

I am trying to create a related post section at the bottom of each post. I am using YARPP plugin to generate the related posts. I want customise the a template to show, a thumbnail(Done), title(Done) and a specific category(Issue).

My issue is its displaying all assigned categories, I only want to display one child category. I have a speaker category with child categories of the names of the speakers.

Let me know if you need more information. Thanks

<h3>Related Posts</h3>
<?php if ( have_posts() ) : ?>
<div>
    <?php 
    while ( have_posts() ) : the_post(); ?>
    <div>
        <?php if ( has_post_thumbnail() ) : ?>
        <?php the_post_thumbnail( $dimensions['size'], array( 'data-pin-nopin' => 'true' ) ); ?></a>
        <a href="<?php the_permalink(); ?>" rel="bookmark norewrite" title="<?php the_title_attribute(); ?>">
        
    </div>

    <div> 
    <?php the_title(); ?>   
        
<!-- How to add specific category -->
        <?php
        $categories = get_the_category();
        $separator = ' ';
        $output = '';

        if ( ! empty( $categories ) ) {
            foreach( $categories as $category ) {
                $term_id = '328';
                $output .= '|| <a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
    }
            echo trim( $output, $separator );
}

?>
<!-- How to add specific category END -->
    </div>  
        
        <?php endif; ?>
    <?php endwhile; ?>
</div>

<?php else : ?>
<p>No related photos.</p>
<?php endif; ?>
1
What is 328? is that id for you want to display?Bhautik

1 Answers

0
votes
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 6, 'post__not_in' => array($post->ID) ) );
    if( $related ) foreach( $related as $post ) { setup_postdata($post); ?>
      <div class="post">

        <div class="post-title">
          <a href="<?php the_permalink() ?>"><h6 class="title"><?php the_title() ?></h6></a>
        </div>
        <a href="<?php the_permalink() ?>"><div class="post-img" style="background-image: url(<?php the_post_thumbnail_url(); ?>)"></div></a>
      </div>
    <?php } wp_reset_postdata(); ?>