2

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.

5
  • What PHP code do you use to fetch the categories?
    – fuxia
    Commented Jan 8, 2013 at 22:45
  • Sorry bout that. Here's a pastie of the catagory code from the loop: pastie.org/5651279
    – hiiambo
    Commented Jan 8, 2013 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.
    – hiiambo
    Commented Jan 8, 2013 at 23:20
  • Don't use external sources - update your question instead.
    – kaiser
    Commented Jan 15, 2013 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
    Commented Jan 15, 2013 at 10:13

3 Answers 3

6
+50

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'

1
  • 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.
    – hiiambo
    Commented Jan 20, 2013 at 16:49
6

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.

1
  • I've amended the second example to use your html markup style, it should do what you want
    – Tom J Nowell
    Commented Jan 18, 2013 at 13:05
0

This code works for me...taxonomy portfolio:

<span class="post-category"><?php _e('In', 'owd'); ?> <?php the_terms( $post->ID, 'portfolio_category', '', ", ", '' ); ?></span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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