Search code examples
phpwordpresstagstaxonomy

Get posts all posts by Tag / URL Wordpress


I'm looking to create a tag.php page, which displays all tags when the user clicks on the tag from a tag cloud. This is what I've got so far, but this seems to display all of my posts.

<article class="articles"> 
<?php
echo '<h2>Tag:';
$tag = single_tag_title();
echo '</h2>';
$args = array(
            'taxonomy' => $tag,
            'terms' => $tag,
);
$postslist = get_posts( $args );?>

<?php foreach( $postslist as $post ) :  setup_postdata($post); ?>


      <div class="clear"></div>
      <span class="timestamp"><?php echo mysql2date('j M Y', $post->post_date) ;?></span></h2>
      <p class="about"><?php the_title(); ?></p>
      <?php the_content(''); ?>
      <?php endforeach;?>
</div>

I cannot seem to figure this out, I've been googling it but I can't seem to find out the information I want...


Solution

  • Your single_tag_title it's not returning to variable:

    $tag = single_tag_title('', false);
    

    try with:

    <?php
    $tag = single_tag_title('', false);
    echo '<h2>Tag: '.$tag.'</h2>';
    
    $args = array(
                'taxonomy' => $tag,
                'terms' => $tag,
    );
    $postslist = get_posts( $args );?>
    
    <?php foreach( $postslist as $post ) :  setup_postdata($post); ?>
    <div class="clear"></div>
          <span class="timestamp"><?php echo mysql2date('j M Y', $post->post_date) ;?></span></h2>
          <p class="about"><?php the_title(); ?></p>
          <?php the_content(); ?>
    </div>
    <?php endforeach;?>