teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

誤字

2018/04/02 16:48

投稿

misato_3310
misato_3310

スコア14

title CHANGED
File without changes
body CHANGED
@@ -179,7 +179,7 @@
179
179
  $term_child = get_term_by( 'id', $term_id, $tax02 );
180
180
 
181
181
  // 子タームのURLを取得
182
- $term_child = sanitize_term( $term_child, $taxonomy );
182
+ $term_child = sanitize_term( $term_child, $tax02 );
183
183
  $term_child_link = get_term_link( $term_child, $tax02 );
184
184
  if ( is_wp_error( $term_child_link ) ) {
185
185
  continue;

1

試した実装内容を追記

2018/04/02 16:48

投稿

misato_3310
misato_3310

スコア14

title CHANGED
File without changes
body CHANGED
@@ -124,4 +124,76 @@
124
124
 
125
125
  ### お願いしたいこと
126
126
  参考になりそうなサイトをしている方、記述の仕方が分かる方などいましたら
127
- ご提案いただけるとうれしいです。
127
+ ご提案いただけるとうれしいです。
128
+
129
+ ### 追記
130
+ いろいろと調べて、実装したい形はこちらの質問([WordPressのカスタム投稿で親カテゴリとサブカテゴリのリストを表示させたい](https://teratail.com/questions/69144))に一番近いと思います。
131
+ なので自分なりに実装できないか試してみたのですが、都道府県カテゴリしか表示されませんでした。
132
+
133
+ ```php
134
+ <?php
135
+ // カスタム分類名
136
+ $tax01 = 'address01';
137
+ $tax02 = 'address02';
138
+
139
+ // パラメータ
140
+ $args = array(
141
+ // 親タームのみ取得
142
+ 'parent' => 0,
143
+
144
+ // 子タームの投稿数を親タームに含める
145
+ 'pad_counts' => true,
146
+
147
+ // 投稿記事がないタームも取得
148
+ 'hide_empty' => false
149
+ );
150
+
151
+ // カスタム分類のタームのリストを取得
152
+ $terms = get_terms( $tax01 , $args );
153
+
154
+ if ( count( $terms ) != 0 ) {
155
+ echo '<div>';
156
+
157
+ // 親タームのリスト $terms を $term に格納してループ
158
+ foreach ( $terms as $term ) {
159
+
160
+ // 親タームのURLを取得
161
+ $term = sanitize_term( $term, $tax01 );
162
+ $term_link = get_term_link( $term, $tax01 );
163
+ if ( is_wp_error( $term_link ) ) {
164
+ continue;
165
+ }
166
+
167
+ // 親タームのURLと名称とカウントを出力
168
+ echo '<h3><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>(' . $term->count . ')</h3>';
169
+
170
+ // 子タームのIDのリストを取得
171
+ $term_children = get_term_children( $term->term_id, $tax02 );
172
+
173
+ if( count( $term_children ) != 0 ) {
174
+ echo '<ul>';
175
+ // 子タームのIDのリスト $term_children を $term_idに格納してループ
176
+ foreach ( $term_children as $term_id ) {
177
+
178
+ // 子タームのIDを元に子タームの情報を取得
179
+ $term_child = get_term_by( 'id', $term_id, $tax02 );
180
+
181
+ // 子タームのURLを取得
182
+ $term_child = sanitize_term( $term_child, $taxonomy );
183
+ $term_child_link = get_term_link( $term_child, $tax02 );
184
+ if ( is_wp_error( $term_child_link ) ) {
185
+ continue;
186
+ }
187
+
188
+ // 子タームのURLと名称とカウントを出力
189
+ echo '<li><a href="' . esc_url( $term_child_link ) . '">' . $term_child->name . '</a>(' . $term_child->count . ')</li>';
190
+ }
191
+ echo '</ul>';
192
+ }
193
+ }
194
+
195
+ echo '</div>';
196
+ }
197
+ ?>
198
+ ```
199
+ 引き続き助言等いただけると幸いです。