Pagination for Sibling Custom Post (Same Node Level?)
-
I can’t figure out how to get pagination working when you need to limit it to the same node level in the post tree.
Let’s say you have the following custom posts for custom post type, Food:
- Drinks
- Snacks
- Chips
- Fritos
- Doritos
- Lays
- Pretzels
- Meals
- Pizza
When you are at Snacks, you would get Drinks as previous link and meals as next link since those are all level 1.
If you are on Snacks->Chips, it would only show pretzels as next as pretzels is a sibling and within the same parent category.
If you are on Snacks->Chips->Doritos it would show Fritos as previous and lays as next.
Hopefully this makes sense. Basically limit the pagination to the same “node” level and if it’s not level 1 then limit it to siblings within the same parent post.
Something along the lines of…
$neighbor_posts = get_posts( array( 'post_type' => $post->post_type, 'sort_column' => 'menu_order', 'sort_order' => 'DESC', 'hierarchical' => 0, ) ); $pages = array(); foreach ($neighbor_posts as $page) { $pages[] += $page->ID; } $current = array_search(get_the_ID(), $pages); $prevID = $pages[$current-1]; $nextID = $pages[$current+1]; ?>
The topic ‘Pagination for Sibling Custom Post (Same Node Level?)’ is closed to new replies.