2011-12-27 46 views
0

我需要组我的文章被标签是这样的:如何按标签分组文章?

TAG1 
Article 1 
Article 2 
Article 5 
TAG2 
Article 3 
Article 4 
TAG3 
Article 6 
Article 7 
Article 8 

我可怎么办呢?

回答

3

这应该工作:

<?php 
    $args = array(
     'orderby'  => 'name', 
     'order'   => 'ASC', 
     'hide_empty' => 1, 
     'taxonomy'  => 'post_tag', //change this to any taxonomy 
    ); 
    foreach (get_categories($args) as $tax) : 
     $args = array(
      'post_type'   => 'post', //change to your post_type 
      'posts_per_page' => -1, 
      'orderby'   => 'title', 
      'orderby'   => 'ASC', 
      'tax_query' => array(
       array(
        'taxonomy' => 'post_tag', //change this to any taxonomy 
        'field'  => 'slug', 
        'terms'  => $tax->slug 
       ) 
      ) 
     ); 
     if (get_posts($args)) : 
    ?> 
     <h2><?php echo $tax->name; ?></h2> 
     <ul> 
      <?php foreach(get_posts($args) as $p) : ?> 
       <li><a href="<?php echo get_permalink($p); ?>"><?php echo $p->post_title; ?></a></li> 
      <?php endforeach; ?> 
     </ul> 
    <?php 
     endif; 
    endforeach; 
?>