Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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..)

share|improve this question

4 Answers

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.

share|improve this answer

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.

share|improve this answer

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.

function wpse5422_the_page_siblings(){
    $post_id = get_the_ID();
    $parent_id = wp_get_post_parent_id( $post_ID );
    $post_type = get_post_type($post_id);

    $sibling_list = get_pages(array(
         'sort_column'=>'menu_order',
         'sort_order' =>'asc',
         'child_of' =>$parent_id,
         'post_type'=> $post_type
    ))
    if( !$sibling_list || is_wp_error($sibling_list) )
        return false;

    $pages = array();
    foreach ($sibling_list as $sibling ) {
         $pages[] = $sibling->ID;
     }

    $current = array_search($post_id, $pages);
    $prevID = isset($pages[$current-1]) ? $pages[$current-1] : false;
    $nextID = isset($pages[$current+1]) ? $pages[$current+1] : false;

    echo wpse5422_display_prev_next($prevID, $nextID);
 }

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:

 function wpse5422_display_prev_next($prevID=false, $nextID=false){
    if( empty($prevID) && empty($nextID) )
       return false;

    $html = '<div class="navigation">';

    if( !empty($prevID) ){
         $html .= '<div class="alignleft">';
         $html .= '<a href="'.get_permalink($prevID).'">Previous</a>';
         $html .= '</div>';
     }

    if( !empty($nextID) ){
         $html .= '<div class="alignright">';
         $html .= '<a href="'.get_permalink($nextID).'">Next</a>';
         $html .= '</div>';
     }

    $html .= '</div><!-- .navigation -->';

    return $html;
}

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();.

share|improve this answer
up vote 0 down vote accepted

All right, here it is, fully working:

<?php
$pagelist = get_pages("child_of=".$post->post_parent."&parent=".$post->post_parent."&sort_column=menu_order&sort_order=asc");
$pages = array();
foreach ($pagelist as $page) {
   $pages[] += $page->ID;
}

$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>

<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="previous">
<a href="<?php echo get_permalink($prevID); ?>" title="<?php echo get_the_title($prevID); ?>">Previous</a>

</div>
<?php }
if (!empty($nextID)) { ?>
<div class="Next">
<a href="<?php echo get_permalink($nextID); ?>" title="<?php echo get_the_title($nextID); ?>">Next</a>
</div>
<?php } ?>
</div>

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...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.