fightstarr20
fightstarr20

Reputation: 12628

Display tags from specific category in WordPress

I am using the code below to attempt to display a list of tags associated with posts in the category 'html'

<ul>
    <?php
        query_posts('category_name=bikes');
        if (have_posts()) : while (have_posts()) : the_post();
             if( get_the_tag_list() ){
                echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
            }
         endwhile; endif; 
         wp_reset_query(); 
    ?>
</ul>

I am not seeing any results when i run it though, I have checked and there are lots of tags associated with the posts in the category.

Can anyone help?

Upvotes: 0

Views: 2778

Answers (2)

harshclimate
harshclimate

Reputation: 11

A better way to get the results you're looking for would be not to use query_posts at all. Rather, use a new query to add to your loop. If my category were named photography, I would use this:

<ul>
    <?php $photographyTags = new WP_Query(array('category_name' => 'photography')); ?>

    <?php if($photographyTags->have_posts()) : while($photographyTags->have_posts()) : $photographyTags->the_post(); ?>
    <?php
        if( get_the_tag_list() ){
            echo get_the_tag_list('<li>','</li><li>','</li>');
        }
    ?>
    <?php endwhile; endif; ?>
    <?php wp_reset_postdata(); ?>
</ul>

Upvotes: 0

Mark
Mark

Reputation: 3271

You'll have to remove the $posttags = since you don't want to assign a variable but output it

<ul>
    <?php
        query_posts('category_name=bikes');
        if (have_posts()) : while (have_posts()) : the_post();
           if( get_the_tag_list() ){
              echo get_the_tag_list('<li>','</li><li>','</li>');
           }
        endwhile; endif; 
        wp_reset_query(); 
    ?>
</ul>

Upvotes: 1

Related Questions