0

I created the tag.php page and when I click on a tag on the page of a post it takes me to the tag.php page with the URL of the tag, for example: / tag / wordpress-1 /.

I want to show all the posts of a the tags without having to set all of them.

'tag' => 'post_tag' did not work, it shows nothing on the page.

But when I set the tag. For example 'tag' => 'wordpress-1' it shows all posts with this tag, but I do not want to have to set all tags, I want it to be automatic.

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array( 'post_type' => 'post', 'posts_per_page' => 9, 'paged' => $paged, 'tag' => 'post_tag' );
    $wp_query = new WP_Query($args);
    while ( have_posts() ) : the_post(); ?>
       <div class="noticia-index">
          <a href="<?php the_permalink(); ?>">
          <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
          <div class="post-thumbnail" style="background: url('<?php echo $backgroundImg[0]; ?>'); background-size: cover; background-position: center;">
          </div></a>
          <div class="noticia-index-conteudo"><a href="<?php the_permalink(); ?>"><h2 class="noticia-titulo"><?php the_title(); ?></h2></a>
          <div class="subtitulo-noticia"><?php the_excerpt(); ?></div>
          <span><img src="<?php echo get_stylesheet_directory_uri(); ?>/img/icones/calendar1.svg" class="calendar"></span>
          <span class="date"><?php echo get_the_date(); ?></span>
          <span><img src="<?php echo get_stylesheet_directory_uri(); ?>/img/icones/clock1.svg" class="clock"></span>
          <span class="time"><?php the_time(); ?></span>
          <span><img src="<?php echo get_stylesheet_directory_uri(); ?>/img/icones/bubble2.png" class="bubble"></span>
          <span class="disqus-comment-count comments" data-disqus-url="<?php the_permalink(); ?>#disqus_thread"></span>
          <a class="leia-mais" href="<?php the_permalink(); ?> ">Leia mais...</a>
         </div>
       </div>

        <?php endwhile; ?>

        <!-- Links de paginação -->
        <?php echo paginate_links( array(

            'prev_text' => '<span>Anterior</span>',
            'next_text' => '<span>Próxima</span>'

        ));
?>

2 Answers 2

0

WP default loop will automatically show the posts with in current tag on tag.php page. you dont need to set custom WP_Query for that but make sure that you are not modifying the loop from any other method.

if( have_posts() ) : while( have_posts() ) : the_post();

   the_title();
  //Rest of code.

endwhile;
endif;
2
  • But I want it to show a specific number of posts and have pagination. How I do without the custom WP_Query? Feb 3, 2017 at 0:01
  • @Michael's answer is what you are looking for. try that code
    – Anwer AR
    Feb 3, 2017 at 5:44
0

But I want it to show a specific number of posts and have pagination. How I do without the custom WP_Query?

if you want to have a different number of posts per page, other than set under 'settings - reading - blog pages show at most [] posts', try working with a 'pre_get_posts' action (remove your custom query);

example code:

function wpse_254946_tag_query( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_tag() ) {
        // Display certain number of posts per page for tag archives
        $query->set( 'posts_per_page', 9 );
        return;
    }
}
add_action( 'pre_get_posts', 'wpse_254946_tag_query', 1 );

your existing pagination function should work with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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