1

I am testing my theme against WordPress theme unit test, which states that:

Large number of categories/tags should not adversely impact layout.

I was able to manage the number of tags but could not help myself in case of categories. Here is the code I'm using to limit the number of tags displayed. Can it be reused somehow for categories or is there any other way possible?

add_filter('widget_tag_cloud_args', 'themename_tag_limit');

//Limit number of tags inside widget
function themename_tag_limit($args){

 if(isset($args['taxonomy']) && $args['taxonomy'] == 'post_tag'){
  $args['number'] = 15; //Limit number of tags
 }

 return $args;
}
  • Your question is not clear to me. On what point you want to limit the categories ? – CodeMascot Dec 9 '16 at 8:36
  • the_dramatist i want to limit the number of categories being displayed in category widget. suppose i have 50 categories in my blog but displaying them all through the category widget will disrupt the blog's layout. so that's why i want to limit number of cats in cat widget. hope its got cleared. – thunder Dec 9 '16 at 9:16
0

You could show the Category widgets as a dropdown instead, if you have large number of categories.

I'm not sure how useful it is to limit categories when ordered by name.

You might try to order by count to display the most used categories:

add_filter( 'widget_categories_args', function( $args )
{
    $args['number']         = 1;
    $args['orderby']        = 'count';
    $args['order']          = 'DESC';
    $args['hierarchical']   = 0;
    $args['hide_empty']     = 1;

    return $args;
} );

but there are other options like include and exclude.

Note that the Category widget uses wp_list_categories() that uses get_categories(), that's a wrapper for get_terms() that uses a WP_Term_Query object that contains the following part, in the WP_Term_Query::get_terms() class method:

  // Don't limit the query results when we have to descend the family tree.
  if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
        if ( $offset ) {
            $limits = 'LIMIT ' . $offset . ',' . $number;
         } else {
             $limits = 'LIMIT ' . $number;
         }
  } else {
      $limits = '';
  }

The dropdown option uses wp_dropdown_categories().

0

You can create your new custom widget. and get category ids of those you want to list and pass them in wp_list_categories function.

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.