WordPress.org

Ready to get started?Download WordPress

Forums

Howto : not display child categories' posts in parent category archive (12 posts)

  1. tikimaticdotcom
    Member
    Posted 7 years ago #

    hi i post this 'cos it's a way to refresh an old post i found on this forum http://wordpress.org/support/topic/71767
    and some of you may be interested...
    Well, the goal is that each category displays only its own posts and not those of its child categories.

    here you go:
    (this in WP v.2.0.4, line numbers may change in further versions)

    1. open wp-includes/classes.php
    2. just insert // at the very beginning of these lines - or destroy them:
      line 473
      $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');

      line 476
      $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');

      line 514
      $whichcat .= get_category_children($q['cat'], " OR category_id = ");

    You're done! Code is poetry, as they say!

    You may then want to do the same with
    wp-includes/wp-includes/template-functions-category.php
    (the changes below are OPTIONAL)

    line 201
    $query .= get_category_children($hide, " AND cat_ID != ");

    And: lines 128-145:

    1- insert /* at the beginning of line 128
    2- insert */ line 145

    128-145:
    function get_category_children($id, $before = '/', $after = '') {
    if ( 0 == $id )
    return '';
    $cat_ids = get_all_category_ids();
    foreach ( $cat_ids as $cat_id ) {
    if ( $cat_id == $id)
    continue;
    $category = get_category($cat_id);
    if ( $category->category_parent == $id ) {
    $chain .= $before.$category->cat_ID.$after;
    $chain .= get_category_children($category->cat_ID, $before, $after);
    }
    }
    return $chain;
    }

  2. spencerp
    Member
    Posted 7 years ago #

    Thanks for updating us on this.. ;) I'm sure others that happen to have the same problem as you had, might learn from this then. ;)

    spencerp

  3. Chionsas
    Member
    Posted 7 years ago #

    Thanks, this was just what I needed =)
    And this should be the default behaviour of WordPress, IMHO.

  4. spencerp
    Member
    Posted 7 years ago #

    Glad to hear that this one was useful Chionsas.. ;) =)

    spencerp

  5. mcjim
    Member
    Posted 7 years ago #

    In WP 2.1.2, comment out line 844 in wp-includes/query.php:

    $whichcat .= get_category_children($q['cat'], '', ', ');

    Is there really no way of doing this without hacking?

  6. CatrionaC
    Member
    Posted 7 years ago #

    I commented out line 844 in WP 2.1.2, and parent categories are still showing child categories' posts. Is there anything else that needs to be changed to get this to work in 2.1.2...? Thanks!

  7. atomictoyguy
    Member
    Posted 7 years ago #

    Same result as CatrionaC here. I would really like to get this working but I can't figure it out for the life of me.

  8. tikimaticdotcom
    Member
    Posted 7 years ago #

    just working on wp 2.2 - the hack is dead!!! Hell & fury !!!! AAArrrh !!! stay tuned - gotta find a way

  9. tikimaticdotcom
    Member
    Posted 7 years ago #

    i'm in as hurry - so the solution for the moment is ti use WP 2.0.10 :-(

  10. CaptApollo
    Member
    Posted 7 years ago #

    I think I've got it for 2.1.3:

    In wp-includes/query.php, modify lines 792-794, truncating lines 792 and 794 as follows (be sure to add the semi-colon on the new end of the line):

    $in_cats .= "$cat, ";
    else
    $out_cats .= "$cat, ";
  11. shiftchip
    Member
    Posted 7 years ago #

    Remove
    $whichcat .= get_category_children($q['cat'], '', ', ');
    from line: 842
    In: wp-includes/query.php

    This is how I did it. Works perfect.

  12. moshu
    Member
    Posted 7 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic