WordPress 自分メモ – archive.phpのカンニングらくがき

前提
投稿 ラベル「投稿」 スラッグ「post」 └category └カテゴリー名「未分類」 カテゴリースラッグ「nocategory」 カテゴリーID「1」 |
カスタム投稿 ラベル「お知らせ」 スラッグ「topics」 └topics_cat └ターム名「スケジュール」 タームスラッグ「schedule」 タームID「3」 |
※人によっては「topics_cat」じゃなくて「topics_category」だったりするかと思うので、いい感じに該当箇所書き換えてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | $thisterm = get_queried_object(); if (is_category()) { $thistax = $thisterm ->taxonomy; $thispt = 'post' ; $thisp_slug = $thisterm ->slug; $thispt_label = esc_html(get_post_type_object(get_post_type())->label); $thispt_slug = esc_html(get_post_type_object(get_post_type())->name); $thisp_label = $thisterm ->name; $thisp_id = $thisterm ->term_id; } else if (is_tax()) { $thistax = $thisterm ->taxonomy; $getthispt = explode ( '_' , $thistax ); $thispt = $thispt [0]; $thisp_slug = $thisterm ->slug; $thispt_label = esc_html(get_post_type_object(get_post_type())->label); $thispt_slug = esc_html(get_post_type_object(get_post_type())->name); $thisp_label = $thisterm ->name; $thisp_id = $thisterm ->term_id; } else { $thispt = $thisterm ->name; $thistax = $thispt == 'post' ? 'category' : $thispt . '_cat' ; $thisp_slug = $thispt ; $thispt_label = esc_html(get_post_type_object(get_post_type())->label); $thispt_slug = esc_html(get_post_type_object(get_post_type())->name); $thisp_label = $thispt_label ; } |
echo $thispt;
is_category | post |
is_tax | topics |
else | post or topics |
echo $thispt_label;
is_category | 投稿 |
is_tax | お知らせ |
else | 投稿 or お知らせ |
echo $thispt_slug;
is_category | post |
is_tax | topics |
else | post or topics |
echo $thisp_label;
is_category | 未分類 |
is_tax | スケジュール |
else | 投稿 or お知らせ |
echo $thisp_slug;
is_category | nocategory |
is_tax | schedule |
else | post or topics |
echo $thistax;
is_category | category |
is_tax | topics_cat |
else | category or topics_cat |
カテゴリー・ターム内のみ
echo $thisp_id;
is_category | 1 |
is_tax | 3 |
$thispt = ‘post’; などなど、あんまり改めて書く意味のないものもちょいちょいありますが、念のため。
これで何をしたいか
1 2 3 4 5 6 7 | <h2><?= $thispt_label ?>のカテゴリ一覧</h2> <?php $terms = get_terms( $thistax ); foreach ( $terms as $term ) { echo '<a href="' .get_term_link( $term ->slug, $thistax ). '">' . $term ->name. '</a>' ; } ?> |
とか。使い方も人生もいろいろ。