Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

jack hamilton
jack hamilton
5,609 Points

How to display posts with a certain tag

I originally asked this question over at stackoverflow with no luck so far so I will cross post it to here if that is okay.

I have found that I cannot seem to be able to locate the posts and I'm thinking I will need to use a custom wp_query loop to locate them but I'm not to sure.

The question.

I'm really struggling at what I am sure is a simple problem. I cannot seem to get the posts which fall under a certain tag to display on that tags page ie: (/tag/blog/) with blog being the Tag.

So far following the Official Tag Templates page on Wordpress I still cannot seem to get it working.

I do not need a heirarchy so tag.php works fine. Using single_tag_title() it does correctly display the tag at the top of the page.

The rest of the Official Tag Templates does not really give much more detail on wether or not I can use the default Loop or a custom one. I have tried with the custom one as shown below but that does not work. (I've kept it down to the minimum not currently worried about styling.)

<?php get_header(); ?>
<p>Tag: <?php single_tag_title(); ?></p>
<div class="container">
    <div id="content" class="clearfix row">
        <div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
            <?php 
                if ( have_posts() ) :
                    while ( have_posts() ) :
                        the_title();
                    endwhile; // end while
                endif; // end if
            ?>
        </div> <!-- end #main -->
    <?php get_sidebar('blog'); // Blog ?>
    </div> <!-- end #content -->
</div>
<?php get_footer(); ?>

So this code currently does display the title of the Tag but not the title of the posts which I am trying to get out.

To wrap the question up. "How do I display the posts which fall under a certain tag."

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

First thing I noticed is you're missing the_post(); in your loop.

So on your line where you have your while loop, append the_post() to it like so

while ( have_posts() ) : the_post();

Try that and see what happens

jack hamilton
jack hamilton
5,609 Points

No still not working. Could it be that the tag is used across 2 custom post types?

Kevin Korte
Kevin Korte
28,149 Points

No, that should be fine. But I didn't realize that we were using custom post types. In that case you'll need to run a WP_Query and pass it the names of the custom post types. Than the rest of the loop should function.