Display posts on tag.php template?

Is it possible to create a tag.php template that when you navigate to the url www.domain.com/tag/architecture/ it will display all the custom posts that have been tagged with that specific tag? And so on for various other tags?

What code will I need to include on my template?

Related posts

Leave a Reply

2 comments

  1. Yes You can create,below is the code i used to display custom post type “knowledge”

    <?php
    global $query_string;
    $posts = query_posts($query_string.'&post_type=knowledge');
    if ( have_posts() ) while ( have_posts() ) : the_post(); 
    ?>
    <a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a>
    <?php endwhile; // end of the loop. ?>
    

    This will help you understand hierarchy of templates:

    http://codex.wordpress.org/images/1/18/Template_Hierarchy.png

    Use of $query_string (example) available here:

    https://developer.wordpress.org/reference/functions/query_posts/

  2. According to the template hierarchy, you can create a file called tag.php which will be used instead of index.php if a tag page is displayed. You can also prepare separate templates for specific tags.

    The best way is to start by creating a copy of your theme’s index.php and calling it tag.php, this way you’ll have some basic code working. Then you can modify tag.php to fit your needs – probably by editing The Loop, or maybe changing some includes if your theme loads the loop from separate file. (but in this case you should consider editing index.php to load some other loop for tag pages – is_tag() may come in handy)