wordpress
Displays top 5 categories ordered by number of posts in a category, includes all parent and child categories.


$args = array(
'type' => 'post',
'orderby' => 'count',   // Orders the list by post count
'order' => 'desc',
'hide_empty' => 1,      // Hides categories with no posts
'number' => 5,          // No of categories to return
'taxonomy' => 'category'
);
$top5_categories = get_categories( $args );

echo "<ul>";
foreach ( $top5_categories as $category ) {
    echo '<li><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . ' (' . $category->count . ') </a></li>';
}
echo "</ul>";

Get only top level categories

Displays top level categories ordered by post count, it doesn’t include child categories. Only Top level categories are returned.


$args = array(
'type' => 'post',
'parent' => 0,          // Gets only top level categories
'orderby' => 'count',   // Orders the list by post count
'order' => 'desc',
'hide_empty' => 1,      // Hides categories with no posts
'number' => 10,         // No of categories to return
'taxonomy' => 'category'
);
$top_categories = get_categories( $args );

echo "<ul>";
foreach ( $top_categories as $category ) {
    echo '<li><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . ' (' . $category->count . ') </a></li>';
}
echo "</ul>";