0

I want to show a simple list of taxonomies associated with my CPT. The taxonomies are "skills". I have the following code:

        <h4>Skills</h4>
            <?php $taxonomy = 'skill';
               $taxonomy_terms = get_terms($taxonomy);
               foreach ($taxonomy_terms as $term) {
               echo '~ '.$term->name .'<br />';
             } ?>

But it's spitting out ALL skills that are added to that CPT, not just the ones selected. I believe the taxonomy code is correct in the functions file because on my CPT listing in the dashboard, it shows the correctly selected skills associated with the corrosponding post. All help is greatly appreciated!

0

I found my own answer. Not sure why this different code works, but it does:

            <h4>Skills</h4>
            <?php
                $terms = wp_get_post_terms($post->ID, 'skill');
                $count = count($terms);
                if ( $count > 0 ) {
                foreach ( $terms as $term ) {
                echo '~ ' . $term->name . "<br /> ";
                }
            }
        ?>
2
  • 1
    I think it is obvious why this code works ant the other not. This code is getting the post terms from the specified taxonomy assigned to the specified post ID. The first code is jus getting the terms from a taxonomy, not the terms assigned to a post. See the code for get_terms() and wp_get_post_terms() functions. – cybmeta May 5 '14 at 21:29
  • @cybnet - thanks for clarifying that! makes perfect sense now. :) – RodeoRamsey May 7 '14 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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