How can I get a Previous / Next navigation that only navigates the child pages of the current page?
By that i mean url.com/page/child1, url.com/page/child2 and so on..
I’ve been searching around alot but I’m still lost.
It seems like you can’t do that according to wordpress (they recommend plugins..)
All right, here it is, fully working:
One little cosmetic thing needs to get fixed, and that is that the “Previous” and “Next” links always should be shown, whether or not there are any more pages…
Great hack, it did what I was looking for.
I thaught some of you might be interrested in what I added, that is: alternative links
the nav is not displayed when not necessary, the styling fits the one the twenty-thirteen theme applies to post navigation.
Untested, but this should work:
First use
get_pages
to find all other pages (or CPT) with the same parent as the current page. Then find the ‘previous’ and ‘next’ pages.The above function must be used inside the loop – it takes the current page (or any hierarchical post type) and finds the previous and next sibling page (i.e. of same parent as current page) according to their menu order (this can be changed to date, or title).
It then uses the following function which takes two IDs as an argument and is simply responsible for producing the output:
Where to put this code
Ideally you should create a plug-in out of it. It will work in functions.php – but really, it shouldn’t be living there.
Usage
Inside the Loop, whether you want to display the page links:
wpse5422_the_page_siblings();
.You could use find the parent page of the current post using post->parent_page, then plug that into get_page_children,obtaining all siblings of the parent page, and then find the next and previous elements in the array that is returned.
If you’re open to going the plugin route, which I personally am for this kind of thing, the plugin Ambrosite Next/Previous Page Link Plus is pretty awesome. It supports the functionality that you’re looking for, plus a lot more.
To show next and previous links independent of whether or not any more pages are present you need to add some conditional statements:
(edited the pointless elseif stuff)
The ‘wpse5422_the_page_siblings()‘ solution works great, I cannot comment as my rep is too low, but my only change is to make this line:
To be this:
This way if the user leaves the sort order of every page as 0, it will default to sorting alphabetical by page title.
Thanks!