Skip to content

Instantly share code, notes, and snippets.

@NeilJS
Created September 27, 2013 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeilJS/6726336 to your computer and use it in GitHub Desktop.
Save NeilJS/6726336 to your computer and use it in GitHub Desktop.
Get array of categories/tags/custom taxonomy objects (as opposed to just outputting html or a list), for further manipulation. This is a slightly modified version of wp_list_categories() - but saved in functions.php!
function wp_get_categories( $args = '' ) {
$defaults = array(
'show_option_all' => '', 'show_option_none' => __('No categories'),
'orderby' => 'name', 'order' => 'ASC',
'style' => 'list',
'show_count' => 0, 'hide_empty' => 1,
'use_desc_for_title' => 1, 'child_of' => 0,
'feed' => '', 'feed_type' => '',
'feed_image' => '', 'exclude' => '',
'exclude_tree' => '', 'current_category' => 0,
'hierarchical' => true, 'title_li' => __( 'Categories' ),
'echo' => 1, 'depth' => 0,
'taxonomy' => 'category'
);
$r = wp_parse_args( $args, $defaults );
if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
$r['pad_counts'] = true;
if ( true == $r['hierarchical'] ) {
$r['exclude_tree'] = $r['exclude'];
$r['exclude'] = '';
}
if ( !isset( $r['class'] ) )
$r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
extract( $r );
if ( !taxonomy_exists($taxonomy) )
return false;
$categories = get_categories( $r );
return $categories;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment