How To Display Different WordPress Metabox Based On Your Taxonomy

Ever since WordPress introduced Custom Post Types and Taxonomy, a different door was open, developers can push the limits of WordPress and creating a theme for different niche becomes easy. Real Estate and Movie Themes came out, and a bunch of others that uses the power of Custom Post Types.

And since we are utilizing the power of Custom Post Types and Taxonomy, displaying them is really a different story. Once of my Client asked me to display two different taxonomy using single.php, since they uses that page to display different things.

Today, I’ll share with you on How To Display Different WordPress Metabox Based On Your Taxonomy, mostly on single.php but you can still using this on page.php or other WordPress Files.

90H

Display Different WordPress Metabox Based On Your Taxonomy

I’ll assume you already know how to build a metabox, if not here’s a list of metabox tutorials and plugins on how to do so. Same with Custom Post Types, you can check out these tutorials on how to do it.

We will utilize the has_term, you can check it out from the codex, here’s a short explanation: Check if the current post has any of the given terms.

On your single.php, under the_content hook we want to display our metabox.

<?php if( has_term( 'acoustics', 'blog_cats' ) ) { ?>
         echo 'DO WANT YOU WANT HERE';
<?php } ?>

 Band is our term and blog_cats is our taxonomy, we will display our metabox according to the term of the taxonomy.

<?php if( has_term( 'acoustic', 'blog_cats' ) ) { ?>
         <?php echo get_post_meta($post->ID, "acoustic_band_name", true); ?>
<?php elseif( has_term( 'alternative', 'blog_cats' ) ) { ?>
         <?php echo get_post_meta($post->ID, "alternative_band_name", true); ?>
<?php elseif( has_term( 'rock', 'blog_cats' ) ) { ?>
        <?php echo get_post_meta($post->ID, "rock_band_name", true); ?>
<?php } ?>

The code above displays different information about the Band, the Client wanted to designed it this way because they are using this part as a sidebar.

You can use this if you have several taxonomy term and you wanted to display different metabox or information about it and you’re using single.php or page.php. I find it really easy to display different information and it saves me a lot of time by not creating another page template.

Hope this is helpful to you, if you have any suggestions to push this further please do so in the comments below. If you have any request, please let me know!