WordPress Get Taxonomies

Wordpress Get TaxonomiesA while back I did a video about the basics of WordPress Taxonomies. Since then I’ve received requests on how to:

  • Display taxonomies for each post
  • Use taxonomies to organize posts
  • Display taxonomies in a drop down list
  • Use custom fields to organize posts
  • Display custom fields in posts
In this tutorial I’ll cover all of those subjects and many more. I go over how to useĀ get_the_term_list() and get_post_meta().
All of this information will be vital to understand how to create a shopping cart in WordPress. All of the code follows the video.

Code From the Video



// Add RSS links to <head> section



// Load jQuery

if ( !is_admin() ) {


wp_register_script(‘jquery’, (“http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js”), false);




// Clean up the <head>

function removeHeadLinks() {

remove_action(‘wp_head’, ‘rsd_link’);

remove_action(‘wp_head’, ‘wlwmanifest_link’);


add_action(‘init’, ‘removeHeadLinks’);

remove_action(‘wp_head’, ‘wp_generator’);


if (function_exists(‘register_sidebar’)) {


‘name’ => ‘Sidebar Widgets’,

‘id’ => ‘sidebar-widgets’,

‘description’ => ‘These are widgets for the sidebar.’,

‘before_widget’ => ‘<div id=”%1$s” class=”widget %2$s”>’,

‘after_widget’ => ‘</div>’,

‘before_title’ => ‘<h2>’,

‘after_title’ => ‘</h2>’




if (function_exists(‘register_nav_menus’)) {

register_nav_menus(array(‘main-menu’ => ‘Main Navigation’));



function catch_that_image() {

global $post, $posts;

$first_img =”;

$output = preg_match_all(‘/<img.+src=[\'”]([^\'”]+)[\'”].*>/i’, $post->post_content, $matches);


$first_img = $matches [1] [0];



$first_img = bloginfo(‘template_directory’);

$first_img .= “/image/default.png”;


return $first_img;



function ntt_create_taxonomies()


register_taxonomy( ‘manufacturer’, ‘post’, array( ‘hierarchical’=>false, ‘label’=>’Manufacturer’, ‘query_var’=>true, ‘rewrite’=>true ));


register_taxonomy( ‘item_number’, ‘post’, array( ‘hierarchical’=>false, ‘label’=>’Item Number’, ‘query_var’=>true, ‘rewrite’=>true ));


register_taxonomy( ‘item_type’, ‘post’, array( ‘hierarchical’=>false, ‘label’=>’Item Type’, ‘query_var’=>true, ‘rewrite’=>true ));


add_action( ‘init’, ‘ntt_create_taxonomies’, 0);



<div id=”firstSidebar”>


<?php if (function_exists(‘dynamic_sidebar’) && dynamic_sidebar(‘Sidebar Widgets’)) : else : ?>


<!– All this stuff in here only shows up if you DON’T have any widgets active in this zone –>


<div class=”sidebarWidgets”>


<div class=”sidebarTitle”> <h4></h4></div>


<p class=”sidebarExcerpt”>

<a href=”<?php bloginfo(‘rss2_url’); ?>”><img src=”<?php bloginfo(‘template_directory’); ?>/images/rss_logo.png” height=”50px” width=”50px”></a>

<a href=”http://www.twitter.com/newthinktank”><img src=”<?php bloginfo(‘template_directory’); ?>/images/twitter_logo.png” height=”50px” width=”50px”></a>

<a href=”http://www.facebook.com/newthinktank”><img src=”<?php bloginfo(‘template_directory’); ?>/images/facebook_logo.png” height=”50px” width=”50px”></a>

<a href=”http://www.linkedin.com/in/derekbanas”><img src=”<?php bloginfo(‘template_directory’); ?>/images/linkedin_logo.png” height=”50px” width=”50px”></a>



<div class=”sidebarTitle”></div>

<p class=”sidebarExcerpt”>

<?php wp_list_pages(‘depth=1&title_li=<div class=”sidebarTitle”><h4>Pages</h4></div>’ ); ?>



<div class=”sidebarTitle”> <h4></h4></div>

<p class=”sidebarExcerpt”>


<img src=”<?php bloginfo(‘template_directory’); ?>/images/ad_sample.png” alt=”Advertising” title=”Advertising” width=”250px” height=”250px” />




<div class=”sidebarTitle”><h4>Categories</h4></div>

<p class=”sidebarExcerpt”>

<?php wp_list_categories(‘depth=1&title_li=’ ); ?>



<form name=’ntt_manu’>



// Function provided by earthmanweb

function get_terms_dropdown($taxonomies, $args){

// Get all values stored in the provided taxonomy

$myterms = get_terms($taxonomies, $args);

// Outputs the dropdown list of taxonomies

// When one is selected the onChange event is triggered and the user changes

// pages

$output =”<select name=’ntt_manu_list’ onChange=’location.href=ntt_manu.ntt_manu_list.options[selectedIndex].value’><option selected>Manufacturers</option>”;

foreach($myterms as $term){

// Get the url for the site

$root_url = get_bloginfo(‘url’);

// Get the taxonomy location


// Get location of each value stored in the taxonomy


// Get the proper name for the taxonomy value

$term_name =$term->name;

$link = $root_url.’/index.php/’.$term_taxonomy.’/’.$term_slug;

$output .=”<option value='”.$link.”‘>”.$term_name.” (“.$term->count.”)</option>”;


$output .=”</select>”;

return $output;



$taxonomies = array(‘manufacturer’);

$args = array(‘orderby’=>’name’,’hide_empty’=>true);

echo get_terms_dropdown($taxonomies, $args);





</div> <!– End of sidebarWidgets –>


<?php endif; ?>




<?php get_header(); ?>



$ntt_plugin_folder = get_bloginfo(‘wpurl’);

$ntt_plugin_folder .= ‘/wp-content/plugins/NTTFeaturedContent2/’;

$ntt_plugin_page = $ntt_plugin_folder . ‘js/featured_data1.txt’;

$ntt_plugin_style = $ntt_plugin_folder . ‘ntt_fc_style2.css’;


<LINK href=”<?php echo $ntt_plugin_style;?>” rel=”stylesheet” type=”text/css”>


<script type=”text/javascript” src=”<?php echo $ntt_plugin_folder; ?>js/featured_content.js”></script>

<div class=”ntt_featured_content”>


$ntt_stored_fc = file_get_contents($ntt_plugin_page);

echo $ntt_stored_fc;




<?php get_sidebar(‘first’); ?>


<div id=”mainContent”>


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>


<div class=”articleContent”>


<div <?php post_class() ?> id=”post-<?php the_ID(); ?>”>


<h3 class=”articleTitle”><a href=”<?php the_permalink(); ?>”><?php the_title();?></a></h3><br />


<div class=”entry”>


<p class=”postExcerpt”>

<?php the_content(); ?>



$ntt_manufacturer = get_the_term_list( $post->ID, ‘manufacturer’, ‘Manufacturer: ‘, ‘, ‘, ”);


$ntt_item_number = get_the_term_list( $post->ID, ‘item_number’, ‘Item Number: ‘, ‘, ‘, ”);


$ntt_item_type = get_the_term_list( $post->ID, ‘item_type’, ‘Item Type: ‘, ‘, ‘, ”);


if (” != $ntt_manufacturer ) {

echo $ntt_manufacturer . “<br />”;



if (” != $ntt_item_type ) {

echo $ntt_item_type . “<br />”;



if (” != $ntt_item_number ) {

echo $ntt_item_number . “<br />”;





$ntt_key = “Price”;

$ntt_price_meta = get_post_meta($post->ID, $ntt_key, true);


if (” != $ntt_price_meta ) {

echo ‘Price: ‘ .$ntt_price_meta . “<br />”;








<?php wp_link_pages(array(‘before’ => ‘Pages: ‘, ‘next_or_number’ => ‘number’)); ?>


<?php the_tags( ‘Tags: ‘, ‘, ‘, ”); ?>












<?php endwhile; endif; ?>


<div class=”morePosts”>


<?php $ntt_current_post = $post;

global $post;


// Get category for the current post

$ntt_post_categories = get_the_category($post->ID);


// If the post has a category start storing them in an array

if ($ntt_post_categories) {

$ntt_category_ids = array();

foreach($ntt_post_categories as $ntt_indiv_category) $ntt_category_ids[] = $ntt_indiv_category->term_id; // Gets the categories id number



‘category__in’ => $ntt_category_ids,

// Don’t show the current post Must pass array

‘post__not_in’ => array($post->ID),

// Don’t show pages

‘post_type’ => ‘post’,

// Only show published posts

‘post_status’ => ‘publish’,

‘posts_per_page’=> 4 // Number of related posts that will be shown.



$my_query = new wp_query( $args );

if( $my_query->have_posts() ) {

echo ‘<div class=”morePosts”><h3>Related Posts</h3>’;

while( $my_query->have_posts() ) {



<div class=”recentPosts”>


<h3 class=”recentTitle”><a href=”<?php the_permalink(); ?>”><?php $ntt_the_title = $post->post_title; echo substr($ntt_the_title, 0, 20); ?></a></h3>


<img src=”<?php echo catch_that_image(); ?>” alt=”<?php the_title(); ?>” width=”150px” height=”150px” class=”recentImage” />


<p class=”recentExcerpt”><?php $ntt_the_excerpt = $post->post_excerpt; echo substr($ntt_the_excerpt, 0, 82); ?>

<a href=”<?php the_permalink(); ?>” class=”clickForMore”>More</a></p>


</div> <!– End of recentPosts div –>



echo ‘</div>’;



$post = $ntt_current_post;

wp_reset_query(); ?>


</div> <!– End of morePosts –>



<?php include (TEMPLATEPATH . ‘/inc/meta.php’ ); ?>


<?php edit_post_link(‘Edit this entry’,”,’.’); ?>


<?php comments_template(); ?>

<br />




<?php get_footer(); ?>

3 Responses to “WordPress Get Taxonomies”

  1. Ben Bunch says:


    Great tutorial, very clear and easy to follow!

    I was looking for a way to integrate my custom taxonomy with the permalink structure.

    For Example:


    Is there a way to set up a term in the permalinks dashboard like

    %taxonomy% ?

    Thanks for an suggestions.


    • admin says:

      I’ll look into that solution. This plugin might get you what you want now though wordpress.org/extend/plugins/permalinks-for-hierarchical-custom-post-types/

  2. ProX says:

    Hi. Nice tut! I am working on it and did this:

    1) made a music_album_type with:
    register_post_type(‘music_album’, $album_args);
    Now i have a menu option with which i can fill posts for music albums.

    2) register_taxonomy with this code:
    ‘music_album’, //(a)

    ‘hierarchical’ => false,
    ‘label’ => ‘Music Style’,
    ‘query_var’ => true,
    ‘rewrite’ => true
    (a) custom post type, so style can be choosen in music_style posts

    And in the sidebar:
    // all your code
    $taxonomies = array(‘music_style’);

    Unfortunately this is not working. The select box appears with the taxonomies and number of post. The link is build (eg: index.php/music_style/disco), but the post can not be found and is not shown.

    If i choose to do this in the side bar:
    $taxonomies = array(‘here a taxonomy which is situated under the posts’);

    then it is working fine.

    So the taxonomy under my own made ‘music_albums’ posts is not working with the provided code, but it is does with the normal posts.

    I made even an taxonomy under the normal posts and that works fine.

    I am new to this material and i hope i did explain my problem right. And of course i woould be very happy if you could tell me what goes wrong?


Leave a Reply

Your email address will not be published.