December 30, 2024
1 min read

We can show all category by simple WordPress default function. like this –

<?php wp_list_categories(); ?>
PHP

It has some Parameters to customize more. you can check details here on wp developer reference

Also we can use PHP loop to show more customize way

<?php 
$categories = get_categories( array(
    'orderby' => 'name',
) );
    
foreach ( $categories as $category ) {
    printf( '<a href="%1$s">%2$s</a><br />',
        esc_url( get_category_link( $category->term_id ) ),
        esc_html( $category->name )
    );
}
?>
PHP

You can also use same parameter here on the array as wp_list_categories. Now what about custom taxonomy? No worries it very simple as previous code.

<?php

// Taxonomy Loop

$terms = get_terms( array(
        'taxonomy'   => 'custom_taxonomy_name', //custom taxonomy name
        'hide_empty' => true, 
));

echo '<ul>';

// Loop through all terms with a foreach loop
foreach( $terms as $term ) {
    // Use get_term_link to get terms permalink
    // USe $term->name to return term name
    echo '<li><a href="'. get_term_link( $term ) .'">'. $term->name .'</a></li>';
}

echo '</ul>'; 
?>
PHP

To Show term inside the wp query loop will be

<?php 
the_terms( int $id, string $taxonomy, string $before = '', string $sep = ', ', string $after = '' ) 
//or simple use
the_terms( get_the_ID() , 'category' ,'', ', ','' );
?>
PHP

Show on single post

 <?php
$terms = get_the_terms( $post->ID , 'custom_taxonomy_name' );
foreach ( $terms as $term ) {
  echo $term->slug;
}
?>
PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Elementor text editor Typography Issue [solution]

Elementor text editor Typography Issue [solution] Font size, color, line height issue fixed

How to Implement Google Ads Conversion Tracking in WooCommerce

Learn how to add Google Ads tracking code to your WooCommerce thank you page effortlessly, without the need for plugins.

JS set interval for an event until element show

Sometimes we need to active an event when a specific element loads on-page or part of an element change.

Adding and Removing Class Based on Element Visibility in the Viewport

Add class on element if it come in view and remove not in view

Web Development Project in mind?

if you looking for a web developer for paid contribution to your project I am available for work.

Mukto
Mukto

Click the button below to chat with me.