Zakhar Martov
asked 4 years ago

Hi, how can i display related post by category, not by tag?

1 Answers
DominicStaff
answered 4 years ago

To resolve this issue, you can open the \wp-content\themes\dw-minion\inc\template-tags.php file. Find the line 124 to line 141.
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 
        );
    } ?>

With new code:

function dw_minion_related_post($post_id) {
    $categories = get_the_category($post->ID);
    if ($categories) {
        $category_ids = array();
        foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
        $args = array (
            'category__in' => $category_ids,
            'post__not_in' => array($post_id),
            'posts_per_page'=> 5, // Number of related posts that will be displayed.
            'ignore_sticky_posts'=>1 
        );
    } else {
        $args = array (
            'post__not_in' => array($post_id),
            'posts_per_page' => 5,
            'ignore_sticky_posts'=>1 
        );
    } ?>

Hope this helps !

Powered by DW Question & Answer Pro