WordPress.org

Ready to get started?Download WordPress

Forums

list_cats / wp_list_cats: hide_empty not hide cats with non-empty sub-cats (4 posts)

  1. I'd like for the hide_empty options in list_cats and wp_list_cats to not hide categories which are empty, but that have sub-categories that have posts in them.

    Or to prevent the function from being changed at it's root, perhaps a configuration option to toggle my above suggestion.

  2. janX
    Member
    Posted 8 years ago #

    I am experiencing the same problem. It occurs whenever a categorie doesn't contain any posts, but one or several of it's subcategories do. Looking at the code it is obvious why this happens (template-functions-category.php, line 322). I can think of a quick fix for this though. Maybe someone can come up with one.

  3. janX
    Member
    Posted 8 years ago #

    I just came up with an idea and here is the result. I hope the support forums' system preserves this in usable form:

    317a318,330
    > if ( $hide_empty ) {
    > $query = "
    > SELECT category_parent, SUM(category_count) AS category_child_count
    > FROM $wpdb->categories
    > WHERE cat_ID > 0 $exclusions
    > GROUP BY category_parent";
    >
    > $cat_child_counts = $wpdb->get_results($query);
    > foreach ( $cat_child_counts as $cat_child_count ) {
    > $category_child_counts["$cat_child_count->category_parent"] = $cat_child_count->category_child_count;
    > }
    > }
    >
    322c335
    < if ( ( intval($hide_empty) == 0 || $category->category_count) && (!$hierarchical || $category->category_parent == $child_of) ) {
    ---
    > if ( ( intval($hide_empty) == 0 || $category->category_count || $category_child_counts[$category->cat_ID]/*cat has non-empty children*/ ) && (!$hierarchical || $category->category_parent == $child_of) ) {

  4. janX
    Member
    Posted 8 years ago #

    The code from the last post is NOT well tested BTW!

Topic Closed

This topic has been closed to new replies.

About this Topic