前提・実現したいこと
現在、WPで「都道府県」「市区町村」「町名・大字」「丁目・字」の4つの
カスタムタクソノミーを作成しました(親子関係ではありません)。
archive-カスタム投稿名.phpに
例)
東京都(10)
L新宿区(5)――北新宿1丁目(5)
L中野区(5)――野方2丁目(5)
のような表示でそれぞれに該当ページへのリンクをつけたいと思っています。
例でいうと
「東京都」というターム名を表示し、「東京都」に属する記事が持つ「新宿区」というターム名を表示し、
「東京都」と「新宿区」に属する記事が持つ「北新宿」というターム名を表示し、
「東京都」と「新宿区」と「北新宿」に属する記事が持つ「1丁目」というターム名を表示したい、ということなのです。
上記の場合、北新宿と野方は「町名・大字」、1丁目と2丁目は「丁目・字」と
別々のタクソノミーとして登録されています。
また、投稿者がWPやパソコンに疎いクライアントのため、
タームの登録時に親カテゴリーを選んだりするのは難しい、という判断で
すべてバラバラに作り、足りなければ投稿画面から追加してもらう、という形をとっています。
なので、親子関係で登録するのは難しく、
またこちらで親子関係で作っておくのも難しいという状況です。
発生している問題
リンクのURL自体は親子関係でない為、パラメーターを渡し表示する。というのは分かっているのですが、
そもそもの組み合わせての表示がうまくいきません。
実装の雰囲気としてはこのページ(複数タクソノミーで絞り込めるアーカイブページを作成する。)が
一番近いのですが、このままやると、それぞれのタクソノミーがばらばらで表示されてしまい、
たとえば1丁目を押すと、全ての市区町村の1丁目の一覧になってしまい、
目的通りのアーカイブができないのです。
該当のソースコード
php
1<div class="shozaichi"> 2 3<table> 4<?php 5 $this_url = get_post_type_archive_link( 'assessment' ); 6 $tax1 = 'address01'; 7 $tax2 = 'address02'; 8 $tax3 = 'address03'; 9 $tax4 = 'address04'; 10 if ( isset( $_GET[$tax1] ) ) $tax1_get = $_GET[$tax1]; else $tax1_get = ''; //URLパラメータを取得 11 if ( isset( $_GET[$tax2] ) ) $tax2_get = $_GET[$tax2]; else $tax2_get = ''; //URLパラメータを取得 12 if ( isset( $_GET[$tax3] ) ) $tax3_get = $_GET[$tax3]; else $tax3_get = ''; //URLパラメータを取得 13 if ( isset( $_GET[$tax4] ) ) $tax4_get = $_GET[$tax4]; else $tax4_get = ''; //URLパラメータを取得 14 15 $tax1_terms = get_terms( $tax1, '&hide_empty=true' ); 16 if ( $tax1_terms ){ 17 $current = ''; 18 if ( $tax1_get == '' ) $current = 'current'; 19 20 foreach( $tax1_terms as $tax1_term ){ 21 $current = ''; 22 if ( $tax1_get == $tax1_term->slug ) $current = 'current'; 23 $tax1_term_items .= '<p><a href="'.$this_url.'?'.$tax1.'='.$tax1_term->slug.'" class="'.$current.'">' .esc_html( $tax1_term->name ). '</a></p>'; 24 } 25 } 26 $tax1_term_items = '<td id="tax1">' . $tax1_term_items. '</td>'; 27 echo $tax1_term_items; 28 29 30 $tax2_terms = get_terms( $tax2, '&hide_empty=true' ); 31 if ( $tax2_terms ){ 32 $current = ''; 33 34 if ( $tax2_get == '' ) $current = 'current'; 35 36 foreach( $tax2_terms as $tax2_term ){ 37 $current = ''; 38 if ( $tax2_get == $tax2_term->slug ) $current = 'current'; 39 $tax2_term_items .= '<p><a href="'.$this_url.'?'.$tax1.'='.$tax1_get.'&'.$tax2.'='.$tax2_term->slug.'" class="'.$current.'">' .esc_html( $tax2_term->name ). '</a></p>'; 40 } 41 } 42 $tax2_term_items = '<td id="tax2">' . $tax2_term_items. '</td>' ; 43 echo $tax2_term_items; 44 45 46 $tax3_terms = get_terms( $tax3, '&hide_empty=true' ); 47 if ( $tax3_terms ){ 48 $current = ''; 49 50 if ( $tax3_get == '' ) $current = 'current'; 51 52 foreach( $tax3_terms as $tax3_term ){ 53 $current = ''; 54 if ( $tax3_get == $tax3_term->slug ) $current = 'current'; 55 $tax3_term_items .= '<p><a href="'.$this_url.'?'.$tax1.'='.$tax1_get.'&'.$tax2.'='.$tax2_get.'&'.$tax3.'='.$tax3_term->slug.'" class="'.$current.'">' .esc_html( $tax3_term->name ). '</a></p>'; 56 } 57 } 58 $tax3_term_items = '<td id="tax3">' . $tax3_term_items. '</td>' ; 59 echo $tax3_term_items; 60 61 62 $tax4_terms = get_terms( $tax4, '&hide_empty=true' ); 63 if ( $tax4_terms ){ 64 $current = ''; 65 66 if ( $tax4_get == '' ) $current = 'current'; 67 68 foreach( $tax4_terms as $tax4_term ){ 69 $current = ''; 70 if ( $tax4_get == $tax4_term->slug ) $current = 'current'; 71 $tax4_term_items .= '<p><a href="'.$this_url.'?'.$tax1.'='.$tax1_get.'&'.$tax2.'='.$tax2_get.'&'.$tax3.'='.$tax3_get.'&'.$tax4.'='.$tax4_term->slug.'" class="'.$current.'">' .esc_html( $tax4_term->name ). '</a></p>'; 72 } 73 } 74 $tax4_term_items = '<td id="tax4">' . $tax4_term_items. '</td>' ; 75 echo $tax4_term_items; 76 77?> 78</table> 79</div> 80
お願いしたいこと
参考になりそうなサイトをしている方、記述の仕方が分かる方などいましたら
ご提案いただけるとうれしいです。
追記
いろいろと調べて、実装したい形はこちらの質問(WordPressのカスタム投稿で親カテゴリとサブカテゴリのリストを表示させたい)に一番近いと思います。
なので自分なりに実装できないか試してみたのですが、都道府県カテゴリしか表示されませんでした。
php
1<?php 2// カスタム分類名 3$tax01 = 'address01'; 4$tax02 = 'address02'; 5 6// パラメータ 7$args = array( 8 // 親タームのみ取得 9 'parent' => 0, 10 11 // 子タームの投稿数を親タームに含める 12 'pad_counts' => true, 13 14 // 投稿記事がないタームも取得 15 'hide_empty' => false 16); 17 18// カスタム分類のタームのリストを取得 19$terms = get_terms( $tax01 , $args ); 20 21if ( count( $terms ) != 0 ) { 22 echo '<div>'; 23 24 // 親タームのリスト $terms を $term に格納してループ 25 foreach ( $terms as $term ) { 26 27 // 親タームのURLを取得 28 $term = sanitize_term( $term, $tax01 ); 29 $term_link = get_term_link( $term, $tax01 ); 30 if ( is_wp_error( $term_link ) ) { 31 continue; 32 } 33 34 // 親タームのURLと名称とカウントを出力 35 echo '<h3><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>(' . $term->count . ')</h3>'; 36 37 // 子タームのIDのリストを取得 38 $term_children = get_term_children( $term->term_id, $tax02 ); 39 40 if( count( $term_children ) != 0 ) { 41 echo '<ul>'; 42 // 子タームのIDのリスト $term_children を $term_idに格納してループ 43 foreach ( $term_children as $term_id ) { 44 45 // 子タームのIDを元に子タームの情報を取得 46 $term_child = get_term_by( 'id', $term_id, $tax02 ); 47 48 // 子タームのURLを取得 49 $term_child = sanitize_term( $term_child, $tax02 ); 50 $term_child_link = get_term_link( $term_child, $tax02 ); 51 if ( is_wp_error( $term_child_link ) ) { 52 continue; 53 } 54 55 // 子タームのURLと名称とカウントを出力 56 echo '<li><a href="' . esc_url( $term_child_link ) . '">' . $term_child->name . '</a>(' . $term_child->count . ')</li>'; 57 } 58 echo '</ul>'; 59 } 60 } 61 62 echo '</div>'; 63} 64?>
引き続き助言等いただけると幸いです。

回答2件
あなたの回答
tips
プレビュー