How to Show Custom Taxonomy in the Custom Post Sidebar?

Sometimes we need to show custom taxonomy in the custom post sidebar. If you already registered a custom post and you want to show custom taxonomy in the custom post sidebar, I think this tutorial will help you. In this tutorial, I will show custom taxonomy(category) in the custom post sidebar. Please follow the below steps.

Step 1: Register Custom taxonomy

For registering custom taxonomy, you need your custom post name. I have already registered a custom post type with the name ‘service’. Please add the below code to your theme functions.php or your theme plugin.

// Custom Taxonomy
function your-domain_custom_post_taxonomy(){
	register_taxonomy(
        'service_cat', // Services taxonomy name
        'service', // Custom post name
            array(
            'hierarchical'      => true,
            'label'             => esc_html__('Services Category', 'text-domain' ),
            'query_var'         => true,
            'show_admin_column' => true,
                'rewrite'       => array(
                'slug'          => 'service-category',
                'with_front'    => true
            )
        )
    );
}
add_action('init', 'your-domain_custom_post_taxonomy');

Step 2: Showing Taxonomy

Please add the below code where you want to show the taxonomy. I have shown this in my custom post sidebar as a list style.

<?php $categories = get_terms( ['taxonomy' => 'service_cat'] ); ?>

<ul>
    <?php foreach($categories as $key => $category) { ?>
        <li>
            <a href="<?php echo esc_url( get_term_link($category) ); ?>">
                <?php echo esc_html($category->name); ?>
            </a>
        </li>
    <?php } ?>
</ul>

Now you can see all custom post categories where you showed.

That’s it! Happy coding. Thanks!

Leave a Reply

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