kazu-h に投稿

wordpress_mod-0.3.1ではsimplelist形式のカテゴリ一覧がデフォルトで
階層表示されるようになっているが、親カテゴリに1つも記事がないと
サブカテゴリも表示されない。
wp-includes/template-functions-category.phpを変更することで対応する。

205行目付近のcategory_description関数定義の直後に以下を追加する。

function get_children_posts($id, &$category_posts) {
  $children_posts = 0;
  if (($str_children = substr(get_category_children($id,\'/\'),1)) != \'\') {
    $children = explode(\'/\', $str_children);
    foreach ($children as $child) {
    if (isset($category_posts["$child"])) {
      $children_posts += $category_posts["$child"];
    }
    }
  }
  return $children_posts;
}

上記追加後の320行?335行目あたりのlist_cats関数内にある、

if (!count($category_posts)) {
    :
    :
}

というif文が閉じる直前に以下を追加する。

  $count_all_children_posts = false;
  foreach ($categories as $category) {
    $children_posts =   get_children_posts($category->cat_ID, $category_posts);
    if ($children_posts > 0) {
      if   ($count_all_children_posts) {
        $category_posts["$category->cat_ID"] += $children_posts;
      } else {
        if (! isset($category_posts["$category->cat_ID"])) {
          $category_posts["$category->cat_ID"] = 0;
        }
      }
    }
  }

以上によって親カテゴリが空でも配下のいずれかのカテゴリに記事が含まれて
いれば、親カテゴリが表示されるようになる。
上記において、$count_all_children_posts = false;とあるがこれをtrueに
することで、親カテゴリの記事数は配下のすべての記事数の合計を計算する
ようになる。
管理画面で設定を変更できるようにするかは当面は考えない。