1

I'm trying to modify the inbuilt WordPress category widget to only show categories if they have children.

I was using this code:

function exclude_widget_subcategories($args){
  $all_categories = get_all_category_ids();

  $exclude_categories = array();

  foreach($all_categories as $category_id){
    $category = get_category($category_id);

if($category->parent!=0){
        $exclude_categories[] = $category_id;
    }
  }
  $exclude = implode(",",$exclude_categories); // The IDs of the excluding categories
  $args["exclude"] = $exclude;
  return $args;
}
add_filter("widget_categories_args","exclude_widget_subcategories");

Which works in that it only shows top-level categories, however my client has various child-categories which also have children themselves and she wants these to show up in the category widget.

Is there a way I can modify this code so that instead of excluding categories which aren't Parent categories it instead excludes categories that don't have children?

0

I would suggest something like this:

function exclude_widget_subcategories($args){
  $all_categories = get_terms('category', array('parent' => 0, 'fields' => 'ids'));

  $exclude_categories = array();

  foreach($all_categories as $category_id){
    $children = get_term_children($category_id, 'category');

    if(count($children)!=0){
        $exclude_categories[] = $category_id;
    }
  }
  $exclude = implode(",",$exclude_categories); // The IDs of the excluding categories
  $args["exclude"] = $exclude;
  return $args;
}
add_filter("widget_categories_args","exclude_widget_subcategories");

get_terms('category', array('parent' => 0, 'fields' => 'ids')); gets all category wordpress terms that are children of '0', ie they are top level items. It also specifies that only the ids are returned, rather than term objects. More documentation here

get_term_children should be fairly self-explanatory, though note that you need to pass the name of the taxonomy to the function call. More documentation here

Please note I haven't tested this out so there may be syntax errors - let me know if there are and I may be able to help further :)

  • Thanks for this! I tried it out and it seems to remove all the parent (top-level) categories - but it also shows all of the child categories, including those without children. So it's definitely doing something, but not really what I want I'm afraid! Client has since clarified their ask, however - turns out they only want to include a specific child category, rather than any child category which has children.... – fionchadd Nov 8 '19 at 14:21
0

My client clarified their ask and it transpired they only wanted to display a specific child-category in the category widget, rather than any child-categories with children of their own.

I modified the code I was using to the following (where 1373 is the ID of the specific sub-category), which has achieved that effect:

function exclude_widget_subcategories($args){
  $all_categories = get_all_category_ids();

  $exclude_categories = array();

  foreach($all_categories as $category_id){
    $category = get_category($category_id);

if($category_id!=1373) {
    if($category->parent!=0){
        $exclude_categories[] = $category_id;
    }
  }
  }
  $exclude = implode(",",$exclude_categories); // The IDs of the excluding categories
  $args["exclude"] = $exclude;
  return $args;
}
add_filter("widget_categories_args","exclude_widget_subcategories");

I'm not sure if this is properly an answer because it doesn't answer the question I initially posed, but it does solve my problem in this case!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.