【问题标题】:How to group articles by tags?如何按标签对文章进行分组?
【发布时间】:2011-12-27 10:17:57
【问题描述】:

我需要按这样的标签对我的文章进行分组:

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

我该怎么做?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    这应该有效:

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

    【讨论】:

      猜你喜欢
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多