カテゴリー、子カテゴリーのサイドバーをカスタマイズしたい
-
カテゴリーページ及び子カテゴリーページへ遷移したときのサイドバーのカスタマイズについてお教えいただけないでしょうか?
親カテゴリーが車だとします。
車の親カテゴリーページのサイドバー表示
車
-子カテゴリー1
-子カテゴリー2
-子カテゴリー3そして子カテゴリー1に遷移したときのサイドバー
車
-子カテゴリー1
-記事タイトル1
-記事タイトル2
-記事タイトル3
-子カテゴリー2
-子カテゴリー3子カテゴリー2に遷移したときのサイドバー
車
-子カテゴリー1
-子カテゴリー2
-記事タイトル1
-記事タイトル2
-記事タイトル3
-子カテゴリー3親カテゴリーの時は子カテゴリー一覧表示、
子カテゴリーに遷移したときはそれに属する子カテゴリーの記事タイトルを表示させたいのですがサイドバーに記述する方法をご存知の方いらっしゃいましたらお教えくださいませ。
-
実は結構長いコードになりそうで、全部を考える余力がないので取り急ぎ考え方のみ。
やることは
いま見てる記事のカテゴリーが何なのかを取得(get_the_category)
↓
取得したカテゴリーが子を持ってるかどうかを判定(if文)
↓子カテゴリーを持っていたら(つまり自分自身が親だったら)、子カテゴリーのリストを出力(wp_list_categoriesに、get_the_categoryで取得した自分のカテゴリーを放り込む)
↓
子カテゴリーを持ってない、つまり自分自身が子だったら、親カテゴリーに属する子カテゴリーを順に出力(get_termsかget_categories)
そのループ中に、自分のカテゴリーと一致した場合は、そのカテゴリーの記事一覧(新着◯件?)を出力
(get_postsに、get_the_categoryで取得した自分のカテゴリーを放り込む)という考え方になるのかなぁと。
もっとスマートなやり方があったらだれかが教えてくれるかも…(/ω・\)チラッチラッ一応今のソース載せます。全然違うと思うんですけども・・・汗
<?php $c = get_the_category(); $pid = $c[0]->parent; if( $pid > 0 ) { do { $t = get_term_by( '', $pid, 'category' ); $pid = $t->parent; } while( $pid > 0 ); echo '<h2 class="h2_side01"><a href="' . get_category_link( $t->term_id ) . '">' . $t->name . '</a></h2>'; }else{ echo '<h2 class="h2_side01"><a href="' . get_category_link( $c[0]->term_id ) . '">' . $c[0]->name . '</a></h2>'; } ?> <ul> <?php $args["parent"] = 12; $cats = get_categories($args); foreach ($cats as $cat) { echo '<h2 class="h2_side01"><a href="' . get_category_link() . '">'.$cat->cat_name.'</a></h2>'; $posts = get_posts("category=".$cat->cat_ID."&orderby=post_modified"); foreach ($posts as $post) : setup_postdata($post); ?> <ul id="catChild"> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> </ul> <?php endforeach; }?> </ul>
現在子カテゴリー一覧までは取得できましたがその先の、子カテゴリーに対しての
記事タイトル取得ができず困っています。どなたかご教授下さいませ。
<?php $c = get_the_category(); $pid = $c[0]->parent; if( $pid > 0 ) { do { $t = get_term_by( '', $pid, 'category' ); $pid = $t->parent; } while( $pid > 0 ); echo '<h2 class="h2_side01"><a href="' . get_category_link( $t->term_id ) . '">' . $t->name . '</a></h2>'; }else{ echo '<h2 class="h2_side01"><a href="' . get_category_link( $c[0]->term_id ) . '">' . $c[0]->name . '</a></h2>'; } ?> <?php $categories = get_terms( 'category', array( 'orderby' => 'asc', 'hide_empty' => 0, 'child_of' => 12, ) ); foreach($categories as $value): ?> <ul id="catChild"><li><a href="<?php echo get_category_link($value->term_id); ?>"><?php echo $value->name;?></a></li></ul> <?php endforeach; ?>
テーマの functions.php に以下を追加:
class My_Walker_Category extends Walker_Category { function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { parent::start_el( $output, $category, $depth, $args, $id ); if ( is_tax() && $category->parent ) { if ( $category->term_id == get_queried_object_id() ) { $posts = get_posts( array( 'nopaging' => true, 'tax_query' => array( array( 'taxonomy' => get_query_var( 'taxonomy' ), 'terms' => $category->term_id, ), ), ) ); if ( ! empty( $posts ) ) { $output .= '<ul>'; foreach ( $posts as $post ) { $output .= sprintf( '<li><a href="%1$s">%2$s</a></li>', get_permalink( $post ), get_the_title( $post ) ); } $output .= '</ul>'; } } } } }
カテゴリーページのサイドバーを表示したいところで下記:
$walker = new My_Walker_Category; echo $walker->walk( get_terms( 'category', 'get=all' ) ), 0, array( 'current_category' => get_queried_object_id(), 'style' => 'list', 'use_desc_for_title' => false, ) );
親切なご回答誠にありがとうございます。
ソースをコピー致しましたが
カテゴリーページのサイドバーを表示したいところの方でエラーが出てしまいました。$walker = new My_Walker_Category; echo $walker->walk( get_terms( 'category', 'get=all' ) ), 0, array( 'current_category' => get_queried_object_id(), 'style' => 'list', 'use_desc_for_title' => false, ) );
)が1つ多いかなと思い外してみましたがうまくいきませんでした。
どうかご教授いただけないでしょうか?やってみたことろ、親カテゴリー取得、子カテゴリー取得しましたがその子カテゴリー取得の時の属するタイトルが取得できませんでした。
また子カテゴリーの記事取得後、single.phpにもまったく同じ表示をそのまま表示することは可能でしょうか?
やってみた記述は下記です。
<ul id="catChild"> <?php $walker = new My_Walker_Category; echo $walker->walk( get_terms( 'category', 'get=all' ) , 0, array( 'current_category' => get_queried_object_id(), 'style' => 'list', 'use_desc_for_title' => false, ) ); ?> </ul>
single.phpにもまったく同じ表示
functions.php:
function my_get_current_term_id() { $pid = 123; // 親カテゴリ(車)のIDに変更してください $id = 0; if ( is_tax() ) { $id = get_queried_object_id(); } else if ( is_single() ) { $terms = get_the_terms( get_the_ID(), 'category' ); $terms = wp_filter_object_list( $terms, array( 'parent' => $pid ) ); $id = reset( $terms )->term_id; } return $id; } class My_Walker_Category extends Walker_Category { function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { parent::start_el( $output, $category, $depth, $args, $id ); if ( ( is_tax() || is_single() ) && $category->parent ) { if ( $category->term_id == my_get_current_term_id() ) { $posts = get_posts( array( 'nopaging' => true, 'tax_query' => array( array( 'taxonomy' => 'category', 'terms' => $category->term_id, ), ), ) ); if ( ! empty( $posts ) ) { $output .= '<ul>'; foreach ( $posts as $post ) { $output .= sprintf( '<li><a href="%1$s">%2$s</a></li>', get_permalink( $post ), get_the_title( $post ) ); } $output .= '</ul>'; } } } } }
サイドバー:
$walker = new My_Walker_Category; echo ' <ul>',$walker->walk( get_terms( 'category', 'get=all' ), 0, array( 'current_category' => my_get_current_term_id(), 'style' => 'list', 'use_desc_for_title' => false, ) ),'</ul> ';
ご回答ありがとうございます。
上記を参考にやってみました。
しかし、子カテゴリーを選んだ時に、それに属する記事タイトルはやはり取得できませんでした。でも、記事まで遷移したら記事タイトル一覧が取得できました。
これをなんとか子カテを選んだ時点でそれに属する記事タイトルだけ表示させたいです。あと、function.phpで親カテのIDを入れましたが、すべてのカテゴリーが表示されてしまいました。
$pid = 123; ←ここ部分ですね。
長い時間かけてしまって申し訳ございませんがよろしくお願いいたします。
諸々問題なくできますよー
サイドバーのソコの前辺りに下記を書いて、
global $wp_query; echo '@@@<pre>'; unset($wp_query->posts); unset($wp_query->post); unset($wp_query->request); var_dump($wp_query); echo '</pre>@@@';
子カテゴリーを選んだ時
に、以下のようになっていますか?
["is_tax"]=> bool(true)
["queried_object"]=> の中身が所望の子カテゴリー
["queried_object"]=> の中身で ["parent"]=> int(123) // 親カテゴリ(車)のID
差し支えなければ出力された ‘@@@’ 間をココに貼付けてみてください。
global $wp_query;
echo ‘@@@'; unset($wp_query->posts); unset($wp_query->post); unset($wp_query->request); var_dump($wp_query); echo '
@@@’;
上記をサイドバーに記述したところ下記のような
文字列がでてきました。
ちなみ親のIDは12です。object(WP_Query)#733 (46) { ["query_vars"]=> array(61) { ["category_name"]=> string(4) "cook" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["tag"]=> string(0) "" ["cat"]=> int(13) ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["comments_popup"]=> string(0) "" ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["post_type"]=> string(0) "" ["posts_per_page"]=> int(10) ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) ["order"]=> string(4) "DESC" } ["tax_query"]=> object(WP_Tax_Query)#2697 (2) { ["queries"]=> array(1) { [0]=> array(5) { ["taxonomy"]=> string(8) "category" ["terms"]=> array(1) { [0]=> string(4) "cook" } ["include_children"]=> bool(true) ["field"]=> string(4) "slug" ["operator"]=> string(2) "IN" } } ["relation"]=> string(3) "AND" } ["meta_query"]=> object(WP_Meta_Query)#2696 (2) { ["queries"]=> array(0) { } ["relation"]=> NULL } ["date_query"]=> bool(false) ["post_count"]=> int(5) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> string(1) "5" ["max_num_pages"]=> float(1) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(true) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(true) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_comments_popup"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash"]=> string(32) "15e893f4c8e5bce8cb333e83bef11205" ["query_vars_changed"]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL ["query"]=> array(1) { ["category_name"]=> string(12) "wedding/cook" } ["queried_object"]=> object(stdClass)#2839 (16) { ["term_id"]=> &int(13) ["name"]=> &string(9) "お料理" ["slug"]=> &string(4) "cook" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(13) ["taxonomy"]=> string(8) "category" ["description"]=> &string(153) "×××" ["parent"]=> &int(12) ["count"]=> &int(5) ["filter"]=> string(3) "raw" ["cat_ID"]=> &int(13) ["category_count"]=> &int(5) ["category_description"]=> &string(153) "×××" ["cat_name"]=> &string(9) "お料理" ["category_nicename"]=> &string(4) "cook" ["category_parent"]=> &int(12) } ["queried_object_id"]=> int(13) }
何度もすいませんがよろしくお願いいたします。
いい忘れていて申し訳ございませんが、制作環境はマルチサイトで制作中でございます。
function.phpなどで、サイトIDなども関わってくるかと思いますが、その辺ところも
お教え願いないでしょうか?
よろしくお願いいたします。
- トピック「カテゴリー、子カテゴリーのサイドバーをカスタマイズしたい」には新たに返信することはできません。