How can i make articles only shows in the category i choose?

Hello,

But i find the articles will automatically shows in: https://www.woolars.com/gearbox/

How can i make the articles only shows in https://www.woolars.com/gearbox/planetary-gearbox/ ?

Thank you 

Comments

  • edited September 2018
    Hello
    By default wordrpess  also show child category post/article in parent category page .

    To remove  child category post from parent category page and want to show only child category article in child category page .
    1 For this install code snippets plugin  . 
    2 Then go to  WP-Admin >> Snippets >>Add new >>  Give title and Copy the code from this code  link and paste inside the code area .
    More  help see the snapshot .
    Refer the link to know more  how to use the code-snippets plugin.
    Let us know for further assistance . 

    Thanks 

  • I already used too much plugin, plugin will make my site slow.

    Is there any solution could let me add code, but do not use plugin?

    Thanks
  • Hi 
    Just add the below  php code in functions.php (Which is located in your theme folder ). 

    function wpse_filter_child_cats( $query ) {

    if ( $query->is_category ) {
    $queried_object = get_queried_object();
    $child_cats = (array) get_term_children( $queried_object->term_id, 'category' );

    if ( ! $query->is_admin )
    //exclude the posts in child categories
    $query->set( 'category__not_in', array_merge( $child_cats ) );
    }

    return $query;
    }
    add_filter( 'pre_get_posts', 'wpse_filter_child_cats' );

    Let us know further assistance . 
  • Dear Akhilesh,

    This code is good, but one problem.

    When i choose both Gearbox and Planetary Gearbox.  The artices will not shows in Gearbo category. (only shows in Planetary gearbox)

    Is it possible  to automatically show the all category I choose(I mean, when i add a new post, if i only choose Planetary Gearbox, it will not shows in Gearbox. If i choose both Gearbox and Planetary Gearbox, it will shows in both. ).  

    Thanks
  • Hi Dear
    You can not take advantage of both features at the time .
    It's not possible to show all the post in both (parent and child )catagory .

    Thanks
    AKHILESH

Sign In or Register to comment.