How to display all the categories for blog posts from WordPress

To display all the categories of posts in WordPress and the active category, you need to go through the following steps:

1. Get the current category:

```code
<?php $current_category = get_queried_object(); ?>
```

2. Get all the categories:

```code
<?php $categories = get_categories(); ?>
```

3. Use a loop to iterate all the categories:

```code
<?php foreach ($categories as $category):
    $category_link = get_category_link($category); ?>
 <a class="blog__category <?php echo $current_category->cat_name ===
 $category->cat_name
     ? "is-active"
     : ""; ?>" 
  href="<?php echo $category_link; ?>">
  <?php echo $category->name; ?>
  </a>
<?php endforeach; ?>
```

Now you can see all the categories that are used on your blog posts and the current selected category.

Roxana Anghel
31 Mar 2022
« Back to post