Recent Posts – select by category

Home Forums WordPress Theme Support Silver Mag Recent Posts – select by category

This topic contains 1 reply, has 2 voices, and was last updated by  Raja CRN 8 years, 11 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #5556

    Asabija
    Member

    Hi there,

    I’m using SilverMag Pro for my website. I’m using the traditional view with Recent Posts. How can I make Recent Posts to select articles from one category only and not all of them.

    Your help is appreciated.

    #5565

    Raja CRN
    Keymaster

    Hi,

    Add the following code in the functions.php file and replace the 5 with your category ID.

    add_action( 'pre_get_posts', 'foo_modify_query_show_category' );
    function foo_modify_query_show_category( $query ) {
        if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
            $query->set( 'cat', '5' );
    }
    

    To find cat id, go to WP > Post > Categories > edit category.. You can find the (ex;tag_ID=5) cat ID like this. Use that number in the above code.

    Thanks.

Viewing 2 posts - 1 through 2 (of 2 total)

Only Verified Customer of this Theme Can post reply to this Support Topic! .