This tutorial will show you how to display posts by post tags in WordPress.
Before you start this tutorial please make sure you learned how to add pages and create page templates in WordPress.
1. Access your theme directory (wp-content/themes/theme###)
2. Create new page, create new page template and assign it to the page.
3. Open the created page template file
To display a posts by specific tag we’ll use the WordPress function "query_posts()"
Let’s create a page template that will display all posts by the tag "custom". The page template file content would be as follows:
<?php /* Template Name: Posts by Tag */ ?> <?php get_header(); ?> <div class="container"> <div class="indent"> <?php query_posts( 'tag=custom' ); if ( have_posts() ) while ( have_posts() ) : the_post(); echo '<li>'; the_title(); echo '</li>'; endwhile; wp_reset_query(); ?> </div> </div> <?php get_footer(); ?>
As you can see we inserted the "query_posts()" function:
query_posts( 'tag=custom' );
Activated the posts loop that will allow WordPress yto display posts:
if ( have_posts() ) while ( have_posts() ) : the_post();
...
endwhile;
Added some HTML markup for the posts list.
echo '<li>'; the_title(); echo '</li>';
And closed the "query_posts()" function after the loop.
wp_reset_query(); ?>