Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




get_categories [ WordPress Function ]

get_categories ( $args = '' )
Parameters:
  • (string|array) $args Optional. Change the defaults retrieving categories.
Links:
See:
Returns:
  • (array) List of categories.
Defined at:



Retrieve list of category objects.

If you change the type to 'link' in the arguments, then the link categories will be returned instead. Also all categories will be updated to be backwards compatible with pre-2.3 plugins and themes.

Source


<?php
function &get_categories$args '' ) {
    
$defaults = array( 'taxonomy' => 'category' );
    
$args wp_parse_args$args$defaults );

    
$taxonomy apply_filters'get_categories_taxonomy'$args['taxonomy'], $args );

    
// Back compat
    
if ( isset($args['type']) && 'link' == $args['type'] ) {
        
_deprecated_argument__FUNCTION__'3.0''' );
        
$taxonomy $args['taxonomy'] = 'link_category';
    }

    
$categories = (array) get_terms$taxonomy$args );

    foreach ( 
array_keys$categories ) as $k )
        
_make_cat_compat$categories[$k] );

    return 
$categories;
}
?>

Examples [ wp-snippets.com ]

Top Google zoekresultaten

Meer ...

2 User Note(s)

Gravatar
Here's an example how to display how many posts are in a category.

$categories = get_categories('include=<IDofCategory>');
foreach ($categories as $cat) {
echo $cat->category_count." posts";
}
Gravatar
This example basically works like wp_list_categories with the only difference that it doesn't show the number posts in this category in parentheses, but the number of comments in the category:

<ul>
<?php
$ausgabe = '';
$allcats = get_categories( array('hide_empty'=>true) );
if( $allcats ) {
foreach( $allcats as $category ) {
$catnum = '';
$catposts = get_posts('numberposts=-1&category=' . $category->term_id);
foreach( $catposts as $catpost ) {
$catnum = $catnum + get_comments_number( $catpost->ID );
}
$cat_name = attribute_escape( $category->name );
$cat_name = apply_filters( 'list_cats', $cat_name, $allcats );
$link = '<a href="' . get_category_link( $category->term_id ) . '" ';
$link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"';
$link .= '>';
$link .= $cat_name . '</a>';
$ausgabe .= '<li class="cat-item cat-item-' . $category->term_id . '">' . $link . ' (' . $catnum . ')</li>' . "\n";
$catposts = array();
}
$ausgabe = apply_filters('wp_list_categories', $ausgabe);
} else {
$ausgabe .= '<li>' . __("No categories") . '</li>';
}
echo $ausgabe;
?>
</ul>

Nieuw toevoegen ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics