カテゴリが下記のような3階層あるとします。
親:A
子:B
孫:C,D,E,F...
孫カテゴリを無視して、親がAかつ子がBカテゴリの記事(投稿日順)で次の記事、前の記事といったページネーションを実現させたいと思っています。
ネットで似たようなソースを見つけて、試したのですが孫カテゴリを除けば実現できたのですが、孫カテゴリが付随すると動きません。
どうしたら良いでしょうか・・・。
【記事1】
親:A
子:B
孫:C
【記事2】
親:A
子:B
孫:D
記事1を表示した場合は、次のページナビゲーションに記事2が表示したい。
記事2を表示した場合は、前のページナビゲーションに記事1のリンクを表示させたい。
<dl> <?php $categories = get_the_category(); foreach ($categories as $category) { if ($category->category_parent === 0) { $parent_term_id = $category->term_id;//親IDを取得 } else { $child_term_id[] = $category->term_id;//子IDを取得 孫含む? } } if (is_array($child_term_id)) { $term_children = get_term_children($parent_term_id, 'category'); $term_children = array_diff($term_children, $child_term_id); $term_children = implode(',', $term_children); } $prev_post = get_previous_post(true, $term_children); $next_post = get_next_post(true, $term_children); if ( !empty( $prev_post ) ): ?> <dt>PREV</dt> <dd> <a href="<?php echo esc_url( get_permalink( $prev_post->ID ) ); ?>"><?php echo $prev_post->post_title; ?></a> </dd> <?php endif; ?> <?php $next_post = get_next_post(true, $term_children); if ( !empty( $next_post ) ): ?> <dt>NEXT</dt> <dd> <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>"><?php echo $next_post->post_title; ?></a> </dd> <?php endif; ?> </dl>
あなたの回答
tips
プレビュー