Skip to content

Instantly share code, notes, and snippets.

@shankar-bavan
Created September 6, 2019 04:11
Show Gist options
  • Save shankar-bavan/a93698413077944c0fb703da7d983816 to your computer and use it in GitHub Desktop.
Save shankar-bavan/a93698413077944c0fb703da7d983816 to your computer and use it in GitHub Desktop.
WordPress Display Categories on Page
<?php
$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' => 4, // No of categories to return
'taxonomy' => 'category'
);
$top_categories = get_categories( $args );
?>
<div class="top-category-title">
<h2>TRENDING CATEGORIES</h2>
</div>
<?php foreach ( $top_categories as $category ) { ?>
<div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
<a href="<?php echo esc_html(get_category_link( get_cat_ID( $category->name ) )) ?>" class="top-category-card">
<h2>
<?php echo esc_html($category->name) ?>
</h2>
<p><?php echo esc_html(wp_trim_words($category->description, 15, '')) ?> </p>
</a>
</div>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment