Jump to content

Changing Related Post Selection From Tags to Categories


JeffCampagna

Recommended Posts

Hey Everyone.


I am hoping this is the right place to ask this question.


I am using a new WordPress theme and it has native related post coding in it. The problem is the code uses Post Tags as the selection value instead of Post Category. I was wondering if anyone could help me with this strip of code. I want to change it so the theme gets Posts in the same Category, instead of Posts with the same Tags.



* Display related posts.
*
* @return void
*/
function stag_related_posts() {
    global $post;
    if( stag_rcp_user_has_no_access() )
        return;
    if( ! stag_theme_mod( 'post_settings', 'show_related_posts' ) )
        return;
    $tags = wp_get_post_tags( $post->ID );
    if ( count( $tags ) ) {
        $tags_ids = array();
        foreach($tags as $individual_tag) {
            $tag_ids[] = $individual_tag->term_id;
        }
        $query = new WP_Query( array(
            'tag__in' => $tags_ids,
            'post__not_in' => array( $post->ID ),
            'posts_per_page' => absint( stag_theme_mod( 'post_settings', 'related_posts_count' ) ),
            'caller_get_posts' => 1
        ) );
        // Add filter to attach 'post-grid' class
        add_filter( 'stag_showing_related_posts', '__return_true' );
        if( $query->have_posts() ) :
            echo '<section class="related-posts" data-layout="2-2-2-2">';
            while( $query->have_posts() ) : $query->the_post();
                get_template_part( 'content', get_post_format() );
            endwhile;
            echo '</section>';
        endif;
    }
    wp_reset_query();
    remove_all_filters( 'stag_showing_related_posts' );
}

I've tried simply replaces the words Tags with Categories, but it doesn't seem to do much.


Any help would be greatly, greatly, appreciated.


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.