LoginSignup
5
2

More than 5 years have passed since last update.

WordPressのカテゴリページで、現在のカテゴリーの子カテゴリー一覧を表示

Posted at

カテゴリページへ来たときに、
ページ上部に子カテゴリを表示し、記事数も出したいとき。

以下関数を使用。

single_cat_title()

wp_list_categories()

<?php
$category_name = single_cat_title('',false); // 現在のカテゴリ名を取得(表示はしないのでfalse)
$category_id = get_cat_ID($category_name); // カテゴリ名から現在のカテゴリIDを取得

$args = array(
  'orderby'            => 'count', // 投稿数順
  'show_count'         => 1, // 投稿数を表示
  'child_of'           => $category_id, // 現在のカテゴリの子カテゴリーを表示
  'title_li'           => '', // タイトル出さない
  'show_option_none'   => '',
  'current_category'   => 0,
  'pad_counts'         => 0,
  'taxonomy'           => 'category', // 投稿のカテゴリを指定
  'walker'             => null
);
?>
<!-- カテゴリ一覧 -->
<ul><?php wp_list_categories( $args ); ?></ul>
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2