Last active
September 3, 2021 19:42
-
-
Save glueckpress/5477286 to your computer and use it in GitHub Desktop.
[WordPress] Pagination for a parent page and their child pages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Pagination for a parent page and their child pages. | |
* Use as gp130428_link_pages() in your template file. | |
* Optionally limit to a particular parent page by passing its ID (i.e. 123) to the function: gp130428_link_pages( 123 ) | |
* | |
*/ | |
function gp130428_paginate_parent_children( $parent = null ) { | |
global $post; | |
$child = $post->ID; | |
$parent = ( null !== $parent ) ? $parent : $post->post_parent; | |
$children = get_pages( array( | |
'sort_column' => 'menu_order', | |
'sort_order' => 'ASC', | |
'child_of' => $parent | |
) ); | |
$pages = array( $parent ); | |
foreach( $children as $page ) | |
$pages[] += $page->ID; | |
if( ! in_array( $child, $pages ) && ! is_page( $parent ) ) | |
return; | |
$current = array_search( $child, $pages ); | |
$prev = $pages[$current-1]; | |
$next = $pages[$current+1]; | |
?> | |
<nav id="nav-single" class="clearfix"> | |
<?php | |
if ( empty( $prev ) && ! is_page( $parent ) ) : | |
?> | |
<div class="previous"><a href="<?php echo get_permalink( $parent ); ?>" title="<?php echo esc_attr( get_the_title( $parent ) ) ?>"><?php echo get_the_title( $parent ) ?></a></div> | |
<?php | |
elseif ( ! empty( $prev ) ) : | |
?> | |
<div class="previous"><a href="<?php echo get_permalink( $prev ); ?>" title="<?php echo esc_attr( get_the_title( $prev ) ) ?>"><?php echo get_the_title( $prev ) ?></a></div> | |
<?php | |
endif; | |
if( ! empty( $next ) ) : | |
?> | |
<div class="next"><a href="<?php echo get_permalink( $next ); ?>" title="<?php echo esc_attr( get_the_title( $next ) ) ?>"><?php echo get_the_title( $next ) ?></a></div> | |
<?php | |
endif; | |
?> | |
</nav> | |
<?php } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
gp130428_paginate_parent_children( 123 )
also includes the parent in the navigation, is there a way to exclude it?