Displaying the current category, taxonomy, or term in WordPress is still a common requirement in modern theme development.
Whether you’re building a blog, WooCommerce store, custom archive page, or dynamic navigation system, there are many situations where you need to retrieve the current category or taxonomy programmatically.
In this guide, we’ll cover the modern ways to:
- get the current post category
- display taxonomy terms
- detect archive categories
- work with WooCommerce product categories
- use
get_queried_object() - follow current WordPress coding standards
Why This Matters
Developers commonly use current category or taxonomy data for:
- breadcrumbs
- related posts
- archive headers
- dynamic navigation
- custom templates
- WooCommerce category pages
- filtering systems
- conditional layouts
Modern WordPress themes also rely heavily on taxonomy-aware template logic, especially with block themes and WooCommerce stores.
Get the Current Post Category
The simplest approach for standard WordPress posts is:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories[0]->name );
}
This retrieves the categories assigned to the current post and outputs the first category name safely.
Display All Categories for a Post
If you want to display all assigned categories:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
foreach ( $categories as $category ) {
echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">';
echo esc_html( $category->name );
echo '</a> ';
}
}
This creates linked category output suitable for blog templates and post metadata sections.
Modern Recommended Method: get_the_terms()
For modern WordPress development, get_the_terms() is often the better choice because it works with both categories and custom taxonomies.
$terms = get_the_terms( get_the_ID(), 'category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
echo esc_html( $term->name );
}
}
This approach is more flexible and future-proof.
Get the Current Taxonomy Term on Archive Pages
When working with category archives, taxonomy archives, or WooCommerce product category pages, use:
$current_term = get_queried_object();
echo esc_html( $current_term->name );
This is now the preferred approach for archive-based templates.
Example: Detect WooCommerce Product Categories
WooCommerce uses the product_cat taxonomy.
To get the current product category:
if ( is_tax( 'product_cat' ) ) {
$current_term = get_queried_object();
echo esc_html( $current_term->name );
}
This is useful for:
- custom WooCommerce archive templates
- category banners
- dynamic headings
- conditional layouts
- product filtering systems
Display WooCommerce Product Categories for Products
To show assigned product categories on single product pages:
$product_terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( ! empty( $product_terms ) && ! is_wp_error( $product_terms ) ) {
foreach ( $product_terms as $term ) {
echo esc_html( $term->name );
}
}
Get the Current Category Slug
Sometimes you need the slug rather than the display name.
$current_term = get_queried_object();
echo esc_html( $current_term->slug );
Useful for:
- CSS body classes
- conditional logic
- JavaScript filtering
- custom template loading
Get the Current Category ID
$current_term = get_queried_object();
echo absint( $current_term->term_id );
This is commonly used for:
- custom queries
- related content
- exclusions
- dynamic filtering
Example: Conditional Layouts by Category
You can customise layouts based on the current category.
$current_term = get_queried_object();
if ( 'news' === $current_term->slug ) {
get_template_part( 'template-parts/category-news' );
}
This approach works well in modern block-compatible themes.
Supporting Yoast Primary Categories
Many WordPress sites use Yoast SEO primary categories.
You can retrieve the primary category like this:
$primary_category = new WPSEO_Primary_Term( 'category', get_the_ID() );
$primary_category_id = $primary_category->get_primary_term();
$term = get_term( $primary_category_id );
if ( ! is_wp_error( $term ) ) {
echo esc_html( $term->name );
}
This is especially useful for:
- breadcrumbs
- SEO-focused themes
- magazine layouts
- news websites
Common Mistakes to Avoid
Using Unescaped Output
Always escape taxonomy output properly:
echo esc_html( $term->name );
And URLs:
echo esc_url( get_term_link( $term ) );
Using get_the_category() Everywhere
get_the_category() only works for standard post categories.
For custom taxonomies and WooCommerce, prefer:
get_the_terms()
Forgetting Archive Contexts
Single posts and archive pages use different logic.
Single post context
get_the_terms()
Archive context
get_queried_object()
Block Themes & Modern WordPress
With Full Site Editing and block themes becoming standard, taxonomy-aware templates are increasingly important.
Modern WordPress development now often combines:
- dynamic block templates
- taxonomy queries
- conditional rendering
- WooCommerce archive logic
- reusable template parts
Understanding taxonomy retrieval properly helps create more flexible themes and WooCommerce stores.
Final Thoughts
Displaying the current category or taxonomy in WordPress is still an essential part of theme and WooCommerce development.
The key is using the right approach for the correct context:
| Scenario | Recommended Function |
|---|---|
| Single post categories | get_the_terms() |
| Standard categories only | get_the_category() |
| Archive pages | get_queried_object() |
| WooCommerce categories | get_the_terms( 'product_cat' ) |
| Current taxonomy archive | get_queried_object() |
Using modern WordPress coding standards, escaped output, and taxonomy-aware logic will make your themes more reliable, scalable, and future-proof.
Need Help With WordPress or WooCommerce Development?
At Urban Soul Design, we build custom WordPress and WooCommerce solutions focused on performance, flexibility, and long-term scalability.
Call 0161 883 0043
