0

How can I only display posts with a specific tag? This is my loop:

<?php 
    if ( have_posts() ) { 
        while ( have_posts() ) { 
            the_post(); 
            get_template_part( 'entry' ); 
            comments_template(); 
            }
        }
?>

I've tried a lot of different things but can't seem to figure it out. I also think I'm missing something vital with how PHP and Wordpress work.

Should I use is_tag and if so, how?

EDIT:

I want to show three columns with posts on the front page (index) each column showing only posts with a specific tag, which is unique for each column. Something like:

| column1 tag1 | column2 tag2 | column3 tag3 |

Thanks in advance!

Sollution with help from Blaine:

<div style="background-color:blue;width:33%;float:right;">
    <?php
        $right = new WP_Query( array(
           'posts_per_page' => -1,
            'no_found_rows'  => true,
            'tag'            => 'right'
        ) );
        if ( $right->have_posts() ) :

            while ( $right->have_posts() ) : $right->the_post();

            the_post(); 
            get_template_part( 'entry' ); 
            comments_template(); 

        endwhile; wp_reset_postdata();

        endif;
    ?>
</div>

<div style="background-color:green;width:33%;float:right;">
    <?php
        $middle = new WP_Query( array(
            'posts_per_page' => -1,
            'no_found_rows'  => true,
            'tag'            => 'middle'
        ) );

        if ( $middle->have_posts() ) :

            while ( $middle->have_posts() ) : $middle->the_post();

            the_post(); 
            get_template_part( 'entry' ); 
            comments_template(); 

            endwhile; wp_reset_postdata();

        endif;
    ?>
</div>

<div style="background-color:yellow;width:33%;">
    <?php
        $left = new WP_Query( array(
            'posts_per_page' => -1,
            'no_found_rows'  => true,
            'tag'            => 'left'
        ) );

        if ( $left->have_posts() ) :

            while ( $left->have_posts() ) : $left->the_post();

            the_post(); 
            get_template_part( 'entry' ); 
            comments_template(); 

            endwhile; wp_reset_postdata();

        endif;
    ?>
</div>

I put this code into my index.php and it works like a charm :)

  • 2
    Where do you want to only display posts with a specific tag, on the home page, archives, ... or somewhere else ? – birgire Nov 9 '15 at 11:14
  • I want to have three columns on the index-page. Each column containing post from one specific tag, unique to that column. Like: | colum1 tag1 | column2 tag2 | column3 tag3 | – TheZeph Nov 9 '15 at 11:34
  • That's a very different question from the one you asked - update your original question with the information you just posted! – TheDeadMedic Nov 9 '15 at 12:02
  • Is it? Though it didn't matter where or how the selection were used. Will update! – TheZeph Nov 9 '15 at 12:20
0

Take a look at the tag parameters of the WP_Query class.

You'll most likely need to create three separate queries.

<?php
$query = new WP_Query( array(
    'posts_per_page' => -1,
    'no_found_rows'  => true,
    'tag'            => 'tag1'
) );

if ( $query->have_posts() ) :

    while ( $query->have_posts() ) : $query->the_post();

        // the_title();

    endwhile; wp_reset_postdata();

endif; ?>

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.