Forked from glueckpress/paginate-parent-children.php
Created
January 18, 2016 22:02
-
-
Save RandyBooth/7ebe33ce0374acdbdafe to your computer and use it in GitHub Desktop.
[WordPress] Pagination for a parent page and their child pages.
This file contains 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