2

How do I create a tag.php template that will work for every post that contain tags?

I created few Tags like = Food, Drink and Fruit

post 1 have Food and Drink tags

post 2 have Food and Fruit tags

How do I create a single page for those post of the tags?

My code looks like this, but it didn't show anything.

function get_tags_post($tag_name){
    $original_query = $wp_query;
    $wp_query = null;
    $brand_name= $tag_name;
    $args=array(
        'posts_per_page'=>5, 
        'tag' => $brand_name
    );
    $wp_query = new WP_Query( $args );
    if ( have_posts() ) : while (have_posts()) : the_post();
        echo '<li>';
        single_tag_title();
        echo '</li>';
    endwhile; endif;
    $wp_query = null;
    $wp_query = $original_query;
    wp_reset_postdata();
}

Please, if anyone knows which part of the code is wrong, tell me.

3

Unfortunately all of your code is wrong. What you are doing is the same as query_posts, which should never be used. Also, never swap the main query on any type of archive page or on the home page for a custom query

To create a tag page, just simply make a copy of your index.php, and rename it tag.php. Here is a copy of the bundled theme, twentyfourteen's tag.php just to give you an idea.

<?php
/**
 * The template for displaying Tag pages
 *
 * Used to display archive-type pages for posts in a tag.
 *
 * @link http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */

get_header(); ?>

    <section id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

            <?php if ( have_posts() ) : ?>

            <header class="archive-header">
                <h1 class="archive-title"><?php printf( __( 'Tag Archives: %s', 'pietergoosen' ), single_tag_title( '', false ) ); ?></h1>

                <?php
                    // Show an optional term description.
                    $term_description = term_description();
                    if ( ! empty( $term_description ) ) :
                        printf( '<div class="taxonomy-description">%s</div>', $term_description );
                    endif;
                ?>
            </header><!-- .archive-header -->

            <?php
                $counter = 1; //Starts counter for post column lay out

                // Start the Loop.
                while ( have_posts() ) : the_post();

        ?>
                <div class="entry-column<?php echo ( $counter%2  ? ' left' : ' right' ); ?>">

                    <?php get_template_part( 'content', get_post_format() ); ?>

                </div>  

        <?php   

            $counter++; //Update the counter

            endwhile;

        pietergoosen_pagination();

        else :
                    // If no content, include the "No posts found" template.
                get_template_part( 'content', 'none' );

                endif;
            ?>
        </div><!-- #content -->
    </section><!-- #primary -->

<?php
get_sidebar( 'content' );
get_footer();

Go and check out the following links as well

EDIT

Just to comment on your code, you should properly indent your code. As your code stands, it makes it hard to read. It is also difficult to debug.

Go and read coding-standards/php/#indentation for more information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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