0

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?

2 Answers 2

1

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>
2
  • weird, it should work according to the reference codex.wordpress.org/Function_Reference/get_the_tag_list
    – Mark
    Commented Jan 6, 2013 at 15:13
  • The only thing I can think about what's maybe going wrong is the if( get_the_tag_list() ) check, try removing it and see if it works
    – Mark
    Commented Jan 6, 2013 at 15:14
0

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>
2
  • post more details about what you tried from your side Commented Apr 1, 2018 at 22:19
  • Well, I'm actually running into some issues. When a tag is used multiple times, the tag duplicates as many times as it's used. Not sure what's up with that? Commented Apr 2, 2018 at 1:04

Your Answer

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

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