通过Tag / URL Wordpress发布所有帖子

时间:2012-09-18 20:57:28

标签: php wordpress tags taxonomy

我正在寻找创建tag.php页面,当用户点击标签云中的标签时,该页面会显示所有标签。这是我到目前为止所得到的,但这似乎显示了我的所有帖子。

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

我似乎无法解决这个问题,我一直在谷歌搜索它,但我似乎无法找到我想要的信息......

2 个答案:

答案 0 :(得分:0)

你的single_tag_title它没有返回变量:

$tag = single_tag_title('', false);

尝试:

<?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;?>

答案 1 :(得分:0)

尝试使用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();

请注意,我还没有测试过这段代码!