質問編集履歴

2

誤字

2018/04/02 16:48

投稿

misato_3310
misato_3310

スコア14

test CHANGED
File without changes
test CHANGED
@@ -360,7 +360,7 @@
360
360
 
361
361
  // 子タームのURLを取得
362
362
 
363
- $term_child = sanitize_term( $term_child, $taxonomy );
363
+ $term_child = sanitize_term( $term_child, $tax02 );
364
364
 
365
365
  $term_child_link = get_term_link( $term_child, $tax02 );
366
366
 

1

試した実装内容を追記

2018/04/02 16:48

投稿

misato_3310
misato_3310

スコア14

test CHANGED
File without changes
test CHANGED
@@ -251,3 +251,147 @@
251
251
  参考になりそうなサイトをしている方、記述の仕方が分かる方などいましたら
252
252
 
253
253
  ご提案いただけるとうれしいです。
254
+
255
+
256
+
257
+ ### 追記
258
+
259
+ いろいろと調べて、実装したい形はこちらの質問([WordPressのカスタム投稿で親カテゴリとサブカテゴリのリストを表示させたい](https://teratail.com/questions/69144))に一番近いと思います。
260
+
261
+ なので自分なりに実装できないか試してみたのですが、都道府県カテゴリしか表示されませんでした。
262
+
263
+
264
+
265
+ ```php
266
+
267
+ <?php
268
+
269
+ // カスタム分類名
270
+
271
+ $tax01 = 'address01';
272
+
273
+ $tax02 = 'address02';
274
+
275
+
276
+
277
+ // パラメータ
278
+
279
+ $args = array(
280
+
281
+ // 親タームのみ取得
282
+
283
+ 'parent' => 0,
284
+
285
+
286
+
287
+ // 子タームの投稿数を親タームに含める
288
+
289
+ 'pad_counts' => true,
290
+
291
+
292
+
293
+ // 投稿記事がないタームも取得
294
+
295
+ 'hide_empty' => false
296
+
297
+ );
298
+
299
+
300
+
301
+ // カスタム分類のタームのリストを取得
302
+
303
+ $terms = get_terms( $tax01 , $args );
304
+
305
+
306
+
307
+ if ( count( $terms ) != 0 ) {
308
+
309
+ echo '<div>';
310
+
311
+
312
+
313
+ // 親タームのリスト $terms を $term に格納してループ
314
+
315
+ foreach ( $terms as $term ) {
316
+
317
+
318
+
319
+ // 親タームのURLを取得
320
+
321
+ $term = sanitize_term( $term, $tax01 );
322
+
323
+ $term_link = get_term_link( $term, $tax01 );
324
+
325
+ if ( is_wp_error( $term_link ) ) {
326
+
327
+ continue;
328
+
329
+ }
330
+
331
+
332
+
333
+ // 親タームのURLと名称とカウントを出力
334
+
335
+ echo '<h3><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>(' . $term->count . ')</h3>';
336
+
337
+
338
+
339
+ // 子タームのIDのリストを取得
340
+
341
+ $term_children = get_term_children( $term->term_id, $tax02 );
342
+
343
+
344
+
345
+ if( count( $term_children ) != 0 ) {
346
+
347
+ echo '<ul>';
348
+
349
+ // 子タームのIDのリスト $term_children を $term_idに格納してループ
350
+
351
+ foreach ( $term_children as $term_id ) {
352
+
353
+
354
+
355
+ // 子タームのIDを元に子タームの情報を取得
356
+
357
+ $term_child = get_term_by( 'id', $term_id, $tax02 );
358
+
359
+
360
+
361
+ // 子タームのURLを取得
362
+
363
+ $term_child = sanitize_term( $term_child, $taxonomy );
364
+
365
+ $term_child_link = get_term_link( $term_child, $tax02 );
366
+
367
+ if ( is_wp_error( $term_child_link ) ) {
368
+
369
+ continue;
370
+
371
+ }
372
+
373
+
374
+
375
+ // 子タームのURLと名称とカウントを出力
376
+
377
+ echo '<li><a href="' . esc_url( $term_child_link ) . '">' . $term_child->name . '</a>(' . $term_child->count . ')</li>';
378
+
379
+ }
380
+
381
+ echo '</ul>';
382
+
383
+ }
384
+
385
+ }
386
+
387
+
388
+
389
+ echo '</div>';
390
+
391
+ }
392
+
393
+ ?>
394
+
395
+ ```
396
+
397
+ 引き続き助言等いただけると幸いです。