続きはWEBで。

技術的なこともそうでないことも、自由気ままに書き留めていくためのサイト

WordPressでカテゴリとその最新記事一覧を表示

カテゴリとそのカテゴリに属する最新記事の一覧を取得する実装例。 サイトマップや関連記事の出力に使えるかもしれない。

カテゴリではなくタグやその他のタクソノミーで表示したい場合は「category」の部分を適当に変更してください。

カテゴリごとに最新記事を3件ずつ表示

出力

・カテゴリ1
 ・カテゴリ1の記事1
 ・カテゴリ1の記事2
 ・カテゴリ1の記事3
・カテゴリ2
 ・カテゴリ2の記事1
 ・カテゴリ2の記事2
 ・カテゴリ3の記事3
・カテゴリ3
 ・カテゴリ3の記事1
 ・カテゴリ3の記事2
...

実装例

<?php
$list = array();
// 取得したいタクソノミー(catgeory、post_tagなど)
$taxonomy  = 'category';
// カテゴリ取得
$args = array(
    'orderby' => 'id',
    'order'   => 'ASC'
);
$terms = get_terms($taxonomy, $args);
// カテゴリごとに記事取得
if($terms){
    foreach($terms as $term){
        $args = array(
            'posts_per_page' => 3,
            'post_type'   => 'post',
            'post_status' => 'publish',
            'tax_query' => array(
                array(
                    'taxonomy' => $taxonomy,
                    'field'    => 'slug',
                    'terms'    => $term->slug,
                    'include_children' => true
                )
            ),
            'orderby' => 'date',
            'order'   => 'DESC'
        );
        $list[] = array(
            'term'   => $term,
            'rposts' => get_posts($args)
        );
    }
}
?>
<!-- カテゴリ一覧を表示 -->
<?php if(!empty($list)): ?>
<ul>
    <?php foreach($list as $data): ?>
    <li>
        <label><a href="<?php echo get_term_link($data['term']); ?>">
            <?php echo $data['term']->name; ?>
        </a><label>
        <?php if($data['rposts']): ?>
        <ul>
            <?php foreach($data['rposts'] as $rpost): ?>
            <li><a href="<?php the_permalink($rpost); ?>">
                <?php echo $rpost->post_title; ?>
            </a></li>
            <?php endforeach; ?>
        </ul>
        <?php endif; ?>
    </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- カテゴリ一覧を表示 -->

上記に加えて階層構造を考慮

以下のような場合に親カテゴリ、子カテゴリごとに最新記事を出力

  • 親カテゴリ1 > 子カテゴリ1
  • 親カテゴリ1 > 子カテゴリ2
  • 親カテゴリ2 > 子カテゴリ3

出力

・親カテゴリ1
 ・子カテゴリ1
  ・子カテゴリ1の記事1
  ・子カテゴリ1の記事2
  ・子カテゴリ1の記事3
 ・子カテゴリ2
  ・子カテゴリ2の記事1
  ・子カテゴリ2の記事2
  ・子カテゴリ2の記事3
・親カテゴリ2
 ・子カテゴリ3
  ・子カテゴリ3の記事1
  ・子カテゴリ3の記事2
  ・子カテゴリ3の記事3
...

実装例

<?php
$list = array();
// 取得したいタクソノミー(catgeory、post_tagなど)
$taxonomy  = 'category';
// 親term取得
$args = array(
    'parent'  => 0,
    'orderby' => 'id',
    'order'   => 'ASC'
);
$pterms = get_terms($taxonomy, $args);
if($pterms){
    foreach($pterms as $index => $pterm){
        $list[$index] = array(
            'term'        => $pterm,
            'children' => array()
        );
        // 子カテゴリ取得
        $args = array(
            'parent'  => $pterm->term_id,
            'orderby' => 'id',
            'order'   => 'ASC'
        );
        $cterms = get_terms($taxonomy, $args);
        // 記事取得
        if($cterms){
            foreach($cterms as $cterm){
                $args = array(
                    'posts_per_page' => 3,
                    'post_type'   => 'post',
                    'post_status' => 'publish',
                    'tax_query' => array(
                        array(
                            'taxonomy' => $taxonomy,
                            'field'    => 'slug',
                            'terms'    => $cterm->slug,
                            'include_children' => true
                        )
                    ),
                    'orderby' => 'date',
                    'order'   => 'DESC'
                );
                $list[$index]['children'][] = array(
                    'term'   => $cterm,
                    'rposts' => get_posts($args)
                );
            }
        }
    }
}
?>
<!-- カテゴリ一覧を表示 -->
<?php if(!empty($list)): ?>
<ul>
    <?php foreach($list as $data): ?>
    <li>
        <label><a href="<?php echo get_term_link($data['term']); ?>">
            <?php echo $data['term']->name; ?>
        </a><label>
        <?php if(!empty($data['children'])): ?>
        <ul>
            <?php foreach($data['children'] as $child): ?>
            <li>
                <label><a href="<?php echo get_term_link($child['term']); ?>">
                    <?php echo $child['term']->name; ?>
                </a><label>
                <?php if($child['rposts']): ?>
                <ul>
                    <?php foreach($child['rposts'] as $rpost): ?>
                    <li><a href="<?php the_permalink($rpost); ?>">
                        <?php echo $rpost->post_title; ?>
                    </a></li>
                    <?php endforeach; ?>
                </ul>
                <?php endif; ?>
            </li>
            <?php endforeach; ?>
        </ul>
        <?php endif; ?>
    </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- カテゴリ一覧を表示 -->