【问题标题】:Get posts all posts by Tag / URL Wordpress按标签/ URL Wordpress 获取所有帖子
【发布时间】:2012-09-18 20:57:27
【问题描述】:

我正在寻找创建一个 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>

我似乎无法弄清楚这一点,我一直在谷歌搜索,但似乎找不到我想要的信息......

【问题讨论】:

    标签: php wordpress tags taxonomy


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2012-08-29
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多