0

So I am trying to list out all the tags created for images uploaded within my custom post type only. I added in a snippet of code to my themes functions.php file to enable media categories and tags:

function wptp_add_tags_to_attachments() {
    register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );

I am using the following code within my template file to generate a list of tags from my custom post type... but hopefully some fresh eyes can show me what I need to do to it to just show the tags I have generated on my images that are uploaded within this custom post type:

        <ul class="work_tags_nav">
            <li class="active"><a class="all">all.</a></li>
        <?php
            query_posts(array( 'post_type'=> 'work', 'taxonomy' => 'type', 'term' => 'case-study' ));
            if ( have_posts() ) : while ( have_posts() ) : the_post();
                $custom_post_tags = get_the_tags();
            if ( $custom_post_tags ) {
                foreach( $custom_post_tags as $tag ) {
                    $tags_arr[] = $tag -> name;
                }
            }
            endwhile; endif;
            if( $tags_arr ) {
                $uniq_tags_arr = array_unique( $tags_arr );
             foreach( $uniq_tags_arr as $tag ) {
                 // LIST ALL THE TAGS FOR DESIRED POST TYPE
                 $sanitizeTag =  sanitize_title($tag);
                 $tag_link = get_terms('name', $tag, 'post_tag');
                 $tag_class = preg_replace('/[^A-Za-z0-9-]+/', '_', html_entity_decode($tag)); // This strips our sepcial characers and spaces, then replaces them with an underscore
                 //echo '<li><a class="'.$tag.'" href="'. get_tag_link($tag_link->term_id).'">' .$tag. '.</a></li>';
                 echo '<li><a class="'.$tag_class.'">' .$tag. '.</a></li>';
                 }
             }
         ?>
        </ul>

Any help would be appreciated.. thanks!

2

get_the_tags() function in deed returns only terms of 'post_tag' taxonomy (tags). If you'd like to get terms of another taxonomies, you'd have to use get_the_terms function (http://codex.wordpress.org/Function_Reference/get_the_terms)

$custom_post_tags = get_the_terms( get_the_ID(), 'type' ); //type is a name of custom taxonomy

or you can use wp_get_object_terms function (http://codex.wordpress.org/Function_Reference/wp_get_object_terms) where you can define more than one taxonomy

$custom_post_tags = wp_get_object_terms( get_the_ID(), array( 'type', 'post_tag', 'category' ) );

Hope I got your question corretly....

  • David, still not getting any results. Basically I am trying to pull an unordered list of all tags within my Media Library used when tagging images that are uploaded. – Danny Sep 16 '13 at 21:03
  • Than you need get_tems function (codex.wordpress.org/Function_Reference/get_terms) – david.binda Sep 17 '13 at 12:16
  • Thanks.. did the trick! get_terms("post_tag", array('exclude' => '110, 120, 118, 115, 117, 119, 116, 132')); – Danny Sep 17 '13 at 20:28

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.