How To Get Categories List In WordPress

There are times when you want to get a list of all the categories of your WordPress site.

How To Get Categories List In WordPress

How To Get Categories List In WordPress

There are times when you want to get a list of all the categories of your WordPress site.

this is very easy to do you can just use one of the built in functions to get all the categories for the site.

The WordPress function we can use is get_categories().
$categories = get_categories( $args );

This will return an array of category objects where you can loop through and get all the information you need.

<?php
$categories = get_categories( array(
    'orderby' => 'name',
    'order'   => 'ASC'
) );
 
foreach( $categories as $category ) {
    $category_link = sprintf( '<a href="%1$s" alt="%2$s">%3$s</a>',
        esc_url( get_category_link( $category->term_id ) ),
        esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
        esc_html( $category->name )
    );
     
    echo '<p>' . sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) . '</p> ';
    echo '<p>' . sprintf( esc_html__( 'Description: %s', 'textdomain' ), $category->description ) . '</p>';
    echo '<p>' . sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . '</p>';
}
?>

Output

categor list

Tags:

Leave a Reply

most readable posts

Filter Posts By Category WordPress

If you have lots of posts in your WordPress site, you have probably used the Category and/or Tag filter at the top of the post. These filter are great because they very quickly allow you to limit the kind of posts that are displayed, and let you find the one(s) you are looking for with […]

Read more
Get Custom Post Type With WP_Query In WordPress

In this post I’ll give you an in-depth look into how WP_Query works – let’s get started! This series on WP_Query, you’ll learn how to use WP_Query to query for posts, pages and custom post types. You can query for specific posts and pages or you can run a query to return posts of one […]

Read more
How To Get Post By Taxonomy In WordPress

Taxonomy are basically a way of grouping data in WordPress. The most common default taxonomy used in WordPress are when grouping posts as either ‘categories’ or ‘tags’ and these are named and recorded in the WordPress database as ‘category’ and ‘post_tag’ respectively. Specific categories or tags are called terms. You can see this image for […]

Read more

About Me

We are glad to see you here… If you are visiting for the first time, then, we hope you’ll stay with us in Touch. Codinglead is all about sincere blogging and spreading some basic and some advanced knowledge to you All. As you can see that here that codinglead.com is working on many different categories and concepts (check out in the Menu).

Our Expertise

  • We are providing the resources of the following technologies:
  • Web Development
  • Web Programming
  • PHP/MySQL
  • Know more.