Last active
May 7, 2017 21:20
-
-
Save Masamasamasashito/ea3453264c51d904285305d7f7d6113e to your computer and use it in GitHub Desktop.
ワードプレスで3階層までカテゴリを固定ページテンプレートで表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- エスケープしていません! --> | |
<?php | |
/* | |
Template Name: カテゴリ一覧固定ページ | |
Template Post Type: page | |
*/ | |
get_header(); | |
?> | |
<!-- カテゴリマップ START --> | |
<div id="category-map"> | |
<?php | |
//第1親階層のカテゴリーすべてのオブジェクトを配列取得 | |
$parent_category_objects = get_categories('parent=0'); | |
//取得した第1親階層の個々カテゴリへの各種処理 | |
foreach($parent_category_objects as $parent_category_object){ | |
//第1親階層の個々カテゴリのIDを取得 | |
$parent_category_id = $parent_category_object->cat_ID; | |
//第1親階層の個々カテゴリのリンクURLを取得 | |
$parent_category_link = get_category_link($parent_category_id); | |
//第1親階層の個々カテゴリのリスト出力 | |
echo '<h2 class="category-parent"><a href="' . $parent_category_link . '">' . $parent_category_object -> name . '</a>'; | |
//親だけの場合はカウント、親と子(孫)の両方にチェックが付いている記事は小孫の数だけでカウント、子(孫)だけの場合もカウント。 | |
$first_category_posts_count = 0; | |
//第1親階層の親カテゴリの記事数を取得 | |
$parent_category_posts_count = $parent_category_object -> count ; | |
//第1親階層の個々カテゴリに対して記事数の有無※本当は0か1か2以上や子(孫)カテゴリ直上かどうかなどもチェックしたほうがいい。 | |
$children_categories_ids = get_term_children($parent_category_id,'category') ; | |
$children_categories_ids_count = count($children_categories_ids); | |
if($parent_category_posts_count > 0){ | |
//親カテゴリ記事有 | |
//子(孫)カテゴリの有無 | |
if($children_categories_ids_count > 0){ | |
//子(孫)カテゴリ有 | |
//子(孫)カテゴリの記事数をカウント | |
foreach ($children_categories_ids as $child_category_id) { | |
$first_category_posts_count += get_category($child_category_id) -> count ; | |
} | |
}else{ | |
//子(孫)カテゴリ無 | |
//親カテゴリの記事数をカウント | |
$first_category_posts_count += $parent_category_posts_count ; | |
} | |
}else{ | |
//親カテゴリ記事無 | |
//子(孫)カテゴリの有無 | |
if($children_categories_ids_count > 0){ | |
//子(孫)カテゴリ有 | |
//子(孫)カテゴリの記事数をカウント | |
foreach ($children_categories_ids as $child_category_id) { | |
$first_category_posts_count += get_category($child_category_id) -> count ; | |
} | |
} | |
} | |
if( $first_category_posts_count!==0 ){echo '('.$first_category_posts_count.')';} | |
echo '</h2>'; | |
//第2階層の子カテゴリのID達を配列で取得。配列の要素数を変数に格納 | |
$first_child_category_ids = get_term_children($parent_category_object->cat_ID,'category'); | |
$first_child_category_counts = count($first_child_category_ids); | |
//第2階層の子カテゴリが存在する場合 | |
if($first_child_category_counts > 0){ | |
echo '<ul>'; | |
//第2階層の子カテゴリの一覧取得条件 | |
$first_category_children_args = array('parent'=>$parent_category_object->cat_ID); | |
//第2階層の子カテゴリーのオブジェクト達を配列取得 | |
$first_child_category_objects = get_categories($first_category_children_args); | |
//第2階層の子カテゴリの数だけリスト出力 | |
foreach($first_child_category_objects as $first_child_category_object){ | |
$first_child_category_id = $first_child_category_object -> cat_ID; | |
$first_child_category_link = get_category_link($first_child_category_id); | |
echo '<li class="cat-item category-child-one"><a href="' . $first_child_category_link . '" class="child-one-a">' . $first_child_category_object -> name .'</a>'; | |
//第2階層の子カテゴリの記事数を初期化 | |
$second_category_posts_count = 0 ; | |
//第2階層の子カテゴリの記事数を取得 | |
$first_child_category_posts_count = $first_child_category_object -> count ; | |
//第2階層の子カテゴリ個々に対して記事数の有無※本当は0か1か2以上や孫カテゴリ直上かどうかなどもチェックしたほうがいい。 | |
$grandson_categories_ids = get_term_children($first_child_category_id,'category') ; | |
$grandson_categories_ids_count = count($grandson_categories_ids); | |
if($first_child_category_posts_count > 0){ | |
//第2階層の子カテゴリ記事有 | |
//孫カテゴリの有無 | |
if($grandson_categories_ids_count > 0){ | |
//孫カテゴリ有 | |
//孫カテゴリの記事数をカウント | |
foreach ($grandson_categories_ids as $grandson_category_id) { | |
$second_category_posts_count += get_category($grandson_category_id) -> count ; | |
} | |
}else{ | |
//孫カテゴリ無 | |
//第2階層の子カテゴリの記事数をカウント | |
$second_category_posts_count += $first_child_category_posts_count ; | |
} | |
}else{ | |
//第2階層の子カテゴリ記事無 | |
//孫カテゴリの有無 | |
if($grandson_categories_ids_count > 0){ | |
//孫カテゴリ有 | |
//孫カテゴリの記事数をカウント | |
foreach ($grandson_categories_ids as $grandson_category_id) { | |
$second_category_posts_count += get_category($grandson_category_id) -> count ; | |
} | |
} | |
} | |
if( $second_category_posts_count!==0 ){echo '('.$second_category_posts_count.')';} | |
//第3階層の孫カテゴリのID達を配列で取得し、配列要素数を変数に格納 | |
$grandson_categories_ids = $first_child_category_object -> cat_ID ; | |
$grandson_categories_count = count(get_term_children($grandson_categories_ids,'category')); | |
//第3階層の孫カテゴリが存在する場合 | |
if($grandson_categories_count > 0){ | |
echo '<ul>'; | |
//第3階層の孫カテゴリの一覧取得条件 | |
$grandson_category_args = array('parent'=>$grandson_categories_ids); | |
//第3階層の孫カテゴリのオブジェクト達を配列取得 | |
$grandson_category_objects = get_categories($grandson_category_args); | |
$grandson_category_objects_pointer = $grandson_category_objects; | |
//第3階層の孫カテゴリの数だけリスト出力 | |
foreach($grandson_category_objects as $grandson_category_object){ | |
$grandson_category_link = get_category_link($grandson_category_object -> cat_ID); | |
echo '<li class="category-child-two"><a href="' . $grandson_category_link . '">' . $grandson_category_object -> name . '</a>('. $grandson_category_object -> count .')</li>'; | |
if(next($grandson_category_objects_pointer)){ | |
echo " , "; // 最後の要素ではないとき | |
} | |
} | |
echo '</ul>'; | |
} | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
} | |
} | |
?> | |
</div><!-- カテゴリマップ END --> | |
<?php get_sidebar(); ?> | |
<?php get_footer(); ?> |
$first_child_second_child_category_posts_count = $first_child_posts_count + $second_child_category_posts_count; も完全に削除、第2階層の子カテゴリと同じように、第3階層も修正。各種変数の命名をフィーリング向けに改修。ワードプレスのコアにあるカテゴリーウィジェットのコアのClassObjectに比べて、まだ記事数の精度に欠けますが、実用で問題は起きないレベルだと思います。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$parent_childs_category_posts_count = $parent_category_posts_count + $childs_category_posts_count; は完全に削除
$category_posts_count = 0; として第1親階層の記事数カウントについて、親カテゴリの記事数有無、子(孫)カテゴリの記事数有無などを分岐