I’ve created a new custom post type (lets call it “Reviews”) on the backend of my WordPress. It works fine for the most part but the Categories I create within that Custom Post Type are not displaying on the meta box.
For example, in a normal post, it should say “In Category X”. In the custom post type, that field is empty. Here are two examples:
Working properly: http://gamingsirs.com/?p=1
Not working: http://gamingsirs.com/?reviews=test
<?php $category_list = get_the_category_list( __( ', ', 'standard' ) ); ?>
<?php if( $category_list ) { ?>
<?php printf( '<span class="the-category">' . __( '%1$s', 'standard' ) . '</span>', $category_list ); ?>
<?php } // end if ?>
You can see the category area is blank. Any suggestions on how to make it show would be greatly appreciated.
Simply use
get_the_term_list
ex:
just change Taxonomy_NAME to your category taxonomy name, if its the default category taxonomy then change it to
'category'
You are currently using
get_the_category_list
, this will list all the terms of the ‘category’ taxonomy. Other taxonomies that come bundled with WordPress by default include tags, they’re not post meta as some people would believe, nor are they dedicated tables.As such they work with all the other taxonomy term APIs. So Instead use
get_term_list
, passing in the name of your review category taxonomy as the second parameter.Also I would not do
if ( $category_list )
, instead it would be less ambiguous to do:if ( !empty( $category_list ) )
e.g. if using with standard categories:
For finer grained control, you can get the term objects themselves rather than a string using
wp_get_object_terms
e.g.
This would allow you to do things such as access term meta using the term ID, which, if you have setup correctly, could be used to store colours and image IDs.
When possible, always use the term oriented functions, rather than the category and tag specific functions. WordPress will end up using them internally anyway to do the same thing, and it makes dealing with custom taxonomies so much easier as you need only learn a single API rather than 2.
This code works for me…taxonomy portfolio: