When reading posts it is common to see links to the next and previous post in the sequence. Something like this:
It’s a nice feature that keeps readers engaged in your content and also aids accessibility by improving navigation. Recently, though, I worked on a project that required adding page (not post) links after the content area on pages that had the same parent (i.e., page siblings). So, something like this:
Instead of manually adding the links, I wrote a little helper function to do the job for me. Here’s how I did it.
What we are doing here is first checking if the current page is a child of
$greatest_ancestor, which is simply the ID of the ancestor page (e.g., parent, great-grandparent, etc.) in question. If so, then we retrieve a list of the current page’s siblings according to
'menu_order' and check where we are in the sequence. Finally, we render the buttons for the previous and next pages in the sequence, if they exist.
To use the above code, simply call it like this:
Mind you, I am using a Genesis hook here. If you are not using Genesis, then you will need to find your theme framework’s appropriate hook (if one exists), or insert
tj_page_navigation_links( $greatest_ancestor ); directly into the desired location within your child theme.
Because the siblings are ordered by
'menu_order' you can conveniently arrange the sequence by changing the “Order” value under Page Attributes, or by using a plugin like Simple Page Ordering.