Ben Clark
asked 10 years ago

I was wondering if there is a way to get the related posts list on a single post to show posts from the same category, rather than displaying a list of recent posts?

3 Answers
DominicStaff
answered 10 years ago

Hi Ben,
To get the related posts listed on a single post, showing posts from the same category, You can open the file template-tags.php in the folder path “wp-content\themes\dw-minion\inc”
, find the line 124 to 158. 
Replace the following code:

 function dw_minion_related_post($post_id) {
    $tags = wp_get_post_tags($post_id);
    if ($tags) {
        $tag_ids = array();
        foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
        $args = array (
            'tag__in' => $tag_ids,
            'post__not_in' => array($post_id),
            'posts_per_page' => 5,
            'ignore_sticky_posts'=>1
        );
    } else {
        $args = array (
            'post__not_in' => array($post_id),
            'posts_per_page' => 5,
            'ignore_sticky_posts'=>1
        );
    } ?>

<?php $related_query = new wp_query( $args ); ?>
    <?php if ( $related_query->have_posts() ) : ?>
    <div class="related-posts">
        <h2 class="related-posts-title"><?php _e( 'Related Articles.', 'dw_minion' ); ?></h2>
        <div class="related-content">
            <?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
            <article class="related-post clearfix">
                <h3 class="related-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
                <div class="related-meta"><time class="related-date"><?php echo get_the_date('d M, Y'); ?></time></div>
            </article>
            <?php endwhile; ?>
        </div>
    </div>
    <?php endif; ?>
<?php wp_reset_query(); ?>
<?php }

With the new code:

 function dw_minion_related_post($post_id) {
 $cate = get_the_category( $post_id );
 $cats = array();
 foreach ($cate as $key => $value) {
  $cats[] = $value->term_id;
 }
 $cats = implode(',',$cats);

 $args = array (
  'cat' => $cats,
  'post__not_in' => array($post_id),
  'posts_per_page' => 5,
  'ignore_sticky_posts'=>1
 );
?>

<?php $related_query = new wp_query( $args ); ?>
 <?php if ( $related_query->have_posts() ) : ?>
 <div class="related-posts">
  <h2 class="related-posts-title"><?php _e( 'Related Articles.', 'dw_minion' ); ?></h2>
  <div class="related-content">
   <?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
   <article class="related-post clearfix">
    <h3 class="related-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    <div class="related-meta"><time class="related-date"><?php echo get_the_date('d M, Y'); ?></time></div>
   </article>
   <?php endwhile; ?>
  </div>
 </div>
 <?php endif; ?>
<?php wp_reset_query(); ?>
<?php }

Hope this helps !

Ben Clark
replied 10 years ago

Perfect!

Thanks

van vadas
answered 10 years ago

Hi, is this possible for the Child Theme, too?

DominicStaff
answered 10 years ago

Hi Van !
Of course ! You can do it with the Child Theme.
Regards,

van vadas
replied 10 years ago

Hi Dominic, I am new with the Child Themes. How do I do it? What I tried was this: I opened the child folder, then created the inc folder ( to maintain the correct order of the directory?? ) and in there copying the complete code from template-tags.php and then replaced the certain part as you explained above. I thought the parent file will be overwritten by the new code? What did I do wrong?

Dominic Staff
replied 10 years ago

I have checked on our demo. It work fine.
Please find here for the document guide on how to create a child theme: https://codex.wordpress.org/Child_Themes
If you still face there issue, please send me username & password of your site for further checking.

Powered by DW Question & Answer Pro