WordPress.org

Ideas

Add the depth parameter to get_categories

  1. mthomps
    Member

    12345

    Currently, the get_categories function takes *most* of the parameters of wp_list_categories, but it lacks a key one: depth. Sometimes, you need more flexibility than wp_list_categories allows, and get_categories works well for that, but it requires some unfortunate hacking to list only one level of subcategories.

    I'm sure folks aren't exactly *clamoring* for this added functionality, but it would plug an odd hole in the code.

    Posted: 9 years ago #
  2. jtlowe22
    Member

    12345

    Please.

    Posted: 8 years ago #
  3. scribu
    Member

    The lack of a depth parameter is due to the way categories are currently stored in the database: it's all or nothing.

    If you want ultimate flexibility, you can create a class that extends Walker_Category and pass it as an argument to wp_list_categories().

    Posted: 8 years ago #
  4. darrinb
    Member

    12345

    But categories have parent IDs, so implementing a depth query shouldn't be terribly difficult. I have a custom script that I use on my site for this exact purpose.

    Posted: 8 years ago #
  5. riledhel
    Inactive

    @dbmartin fill a ticket in trac for an enhancement and add your custom script as a patch. That way it's easier to add the code if they want to.

    Posted: 8 years ago #
  6. scribu
    Member

    But categories have parent IDs, so implementing a depth query shouldn't be terribly difficult. I have a custom script that I use on my site for this exact purpose.

    You have to do a separate query for each level. That doesn't scale very well.

    Posted: 8 years ago #
  7. scribu
    Member

    The tiny few who want that ability can use a plugin.

    Posted: 8 years ago #
  8. ambrosite
    Member

    scribu,

    "If you want ultimate flexibility, you can create a class that extends Walker_Category and pass it as an argument to wp_list_categories()."

    How would that work? From what I can see, the call to the Walker_Category class, by way of the walk_category_tree function, is hard-coded into wp_list_categories (~line 486 of category-template.php). The only way I can see to get wp_list_categories to use a different Walker class is to rewrite it.

    Posted: 8 years ago #
  9. ambrosite
    Member

    Never mind, I figured it out. I must say though, that is really obscure. There is no mention of the 'walker' parameter in the Codex. If you hadn't brought it up, I might never have noticed it.

    Posted: 8 years ago #
  10. scribu
    Member

    Suggest closing as "Not Core".

    Posted: 8 years ago #

RSS feed for this topic

Reply »

You must log in to post.

  • Rating

    12345
    50 Votes
  • Status

    This is not a core suggestion