0

I am using Custom Post Type UI and Custom Fields plugins for the WordPress theme I am creating.

I have created a custom post type of price and I have created a custom taxonomy called price_types using Custom Post Type UI.

On my Prices page I can loop through the built in taxonomy 'Categories' easily using the code below:

$categories = get_terms('category');
foreach( $categories as $category ):
?>
  <section class="pricing">
    <h2><?php echo $category->name; // Print the cat title ?></h2>

  </section>
<?php endforeach; ?>

If I try and do the same for my custom taxonomy even though I have added entries I get nothing?

$categories = get_terms('price_types');
foreach( $categories as $category ):
?>
  <section class="pricing">
    <h2><?php echo $category->name; // Print the cat title ?></h2>

  </section>
<?php endforeach; ?>

Does anyone have any ideas what I am doing wrong?

4
  • do you get anything if you echo print_r($category); I take it the remming '//' is not in your actual code?
    – Joshc
    Nov 26, 2013 at 16:30
  • or is it maybe... codex.wordpress.org/Function_Reference/get_the_terms
    – Joshc
    Nov 26, 2013 at 16:34
  • I don't get anything when using echo print_r($category); the // comment is not in my code.
    – PaulS
    Nov 26, 2013 at 16:49
  • did you try get_the_terms() instead of get_terms() - actually sorry ignore this if it works for categorys then surely the loop should work. Let me test it on mine.
    – Joshc
    Nov 26, 2013 at 17:00

1 Answer 1

1

Try this, this should retrieve all taxonomies terms including empty ones.

<?php
        $terms = get_terms('price_types',
            array(
                'hide_empty' => false,
                'orderby'    => 'name',
                'order'      => 'ASC'
            )
        );
        echo print_r($terms);
        foreach( $terms as $term ):
    ?>
  <section class="pricing">         
    <h2><?php echo $term->name; ?></h2>
  </section>
<?php endforeach; ?>
2
  • that seemed to do the trick! Array ( [0] => stdClass Object ( [term_id] => 6 [name] => Ladies Styling [slug] => ladies-styling [term_group] => 0 [term_taxonomy_id] => 11 [taxonomy] => price_types [description] => [parent] => 0 [count] => 0 ) ) 1 Ladies Styling Does this mean that the term is empty? What does this actually mean?
    – PaulS
    Nov 27, 2013 at 8:30
  • Basically echo print_r($terms) shows the contents of the variable $terms you can remove this line echo print_r($terms). It's only to test. I think maybe because your terms are empty, this is why they would not display, so 'hide_empty' => false fixed this.
    – Joshc
    Nov 27, 2013 at 9:41

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.