Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
What PHP code do you use to fetch the categories? –  toscho Jan 8 '13 at 22:45
    
Sorry bout that. Here's a pastie of the catagory code from the loop: pastie.org/5651279 –  Bo Shubinsky Jan 8 '13 at 22:51
    
Whoops. Found that that function I was using listed EVERY category, not just the one it was in. Still looking for a solution. –  Bo Shubinsky Jan 8 '13 at 23:20
    
Don't use external sources - update your question instead. –  kaiser Jan 15 '13 at 0:07
    
By category what are you referring to? A custom taxonomy? If so what is it called and can we see the registration code –  Tom J Nowell Jan 15 '13 at 10:13
add comment

3 Answers

Simply use get_the_term_list

ex:

echo get_the_term_list( $post->ID, Taxonomy_NAME, '<span class="the-category">', ', ', '</span>' );

just change Taxonomy_NAME to your category taxonomy name, if its the default category taxonomy then change it to 'category'

share|improve this answer
    
How would I use it to display multiple taxonomy names? If I use this in place of what I have, it breaks then normal non-custom post types. –  Bo Shubinsky Jan 20 '13 at 16:49
add comment

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:

global $post;
$terms =  get_the_term_list( $post->ID, 'category', '<span class="the-category">', ', ', '</span>' );
if( !empty($terms) ){
    echo $terms;
} else {
    // none found?
}

For finer grained control, you can get the term objects themselves rather than a string using wp_get_object_terms

e.g.

$terms = wp_get_object_terms($post->ID, 'category');
if(!empty($terms)){
    if(!is_wp_error( $terms )){
        foreach($terms as $term){
            echo '<span class="the-category"><a href="'.get_term_link($term->slug, 'category').'">'.$term->name.'</a></span>'; 
        }
    }
} else {
    // no terms found
}

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.

share|improve this answer
    
I've amended the second example to use your html markup style, it should do what you want –  Tom J Nowell Jan 18 '13 at 13:05
add comment

This code works for me...taxonomy portfolio:

<span class="post-category"><?php _e('In', 'owd'); ?> <?php the_terms( $post->ID, 'portfolio_category', '', ", ", '' ); ?></span>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.