0

I have a Custom Post Type set up with a Custom Taxonomy. For this Custom Taxonomy, I'm trying to figure out how to echo out the titles of the parent category and current child category within it.

This is the set-up:

prints (registered custom post type)

print_type (registered custom taxonomy)

  • Prints & Note Cards (parent category in print_type taxonomy)
    • Floral Prints (child category)
    • Scenic Prints (child category)
    • Note Cards (child category)
    • Selection (child category)

On single-prints.php I would like to display (individually, not as a list):

Parent Category / Current Child Category / Post Title

None of that needs to be linked, I simply want the text in order to display the hierarchy. I've tried dozens of bits of code to display the two types of category titles without any luck.

I'm new to WP and PHP and I'm very frustrated since can't seem to wrap my head around what I'm doing that's wrong. Here's the page code:

<?php get_header(); ?>

<div id="content">

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

    <h2>
    <?php
        $category = get_the_category(); 
        echo $category[0]->cat_name;
    ?>
    </h2>

    <h3><?php echo get_category_parents(19, TRUE, ' / '); ?><?php the_title(); ?></h3>

    <p><?php the_excerpt(); ?></p>

    <div id="workgrid" class="<?php the_ID(); ?>"><?php the_content(); ?></div>

<?php endwhile; endif; ?>

</div>
<!-- END CONTENT -->

<?php get_sidebar('prints'); ?>

<div class="clear"></div>

2 Answers 2

1

Are you sure you are not using a custom taxonomy in your custom post type?

Taxonomies for Custom Post Types have to use get_terms instead of get_category. More information on get_terms from the WordPress Codex: http://codex.wordpress.org/Function_Reference/get_terms

2
  • You're right, I am using custom taxonomies. Prints is the CPT with custom taxonomies of Floral Prints, Scenic Prints, and Note Cards. Although, I'm unsure exactly how to edit that block of code to use get_terms instead of get_the_category and get_the_category_parents.
    – dugost
    Oct 6, 2011 at 2:31
  • I've clarified my problem and corrected some terminology, hoping to make things clearer.
    – dugost
    Oct 6, 2011 at 18:02
0

just a single question, how do you want to set a category to a post-type? post-type are already categories (the name of the post-type), that's probably the reason cause is not returning any value.

Just try the same code, however, make this template for a specific category (single) not post-type, and I think, you will get the result you want.

2
  • I have my terminology mixed up. I'm using custom taxonomies with my CPTs. For the custom tax of Floral Prints, for instance, I used single-floral-prints.php and didn't see any difference. Though it appears I need to be using get_terms instead of get_the_category.
    – dugost
    Oct 6, 2011 at 2:34
  • I've clarified my problem and corrected some terminology, hoping to make things clearer.
    – dugost
    Oct 6, 2011 at 18:04

Your Answer

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

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