WPのPHPで不明な点があります。PHP初心者なので、質問の内容もおかしいかもしれませんが宜しくお願いいたします。
親カテゴリーページには子カテゴリーのみを表示させたいですが、孫カテゴリーも表示されてしまいます。
子カテゴリーのページに入ると孫カテゴリーを表示するように分岐させたいです。
###発生している問題・エラーメッセージ
エラーメッセージはとくに出ていません
###該当のソースコード
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); if ( $term->childlen ) { // 子 $posts = get_posts( array( 'taxonomy' => get_query_var( 'taxonomy' ), 'term' => get_query_var( 'term' ), 'hierarchical' => 0, 'exclude_tree' => '', 'child_of' => 0, ) ); ?><?php wp_list_categories('depth=1'); ?><ul><?php foreach ( (array) $posts as $post ) : setup_postdata( $post ); ?> <li><a href="%1$s">%2$s</a></li> <?php echo esc_url( get_permalink( get_the_ID() ) ); ?> <?php echo esc_html( get_the_title( get_the_ID() ) ); ?> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); } else { //孫 wp_list_categories( array( 'taxonomy' => get_query_var( 'taxonomy' ), 'child_of' => $term->term_id, 'exclude_tree' => '', ) ); } ?>
###試したこと
いろいろな記事を参考にいじってみましたが、できませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
WordPress 4.2.2
回答2件
あなたの回答
tips
プレビュー