0

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...

Max Chandler
  • 503
  • 1
  • 6
  • 21

2 Answers2

0

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;?>
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

Try using wp_query

$tag = single_tag_title('', false);
echo '<h2>Tag: '.$tag.'</h2>';

// The Query
$the_query = new WP_Query( $tag );

// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
            $the_query->the_post();
    $post = get_queried_object();
            echo "<div class='clear'></div>
  <span class='timestamp'>" . mysql2date('j M Y', $post->post_date) . " </span></h2>
  <p class='about'" . the_title() . "</p>";
  <?php the_content(); ?>
</div>
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Please note, I haven't tested this code!

Talk nerdy to me
  • 1,085
  • 9
  • 11