Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browsing page siblings through next/previous links

Tags:

wordpress

I'm using WordPress as CMS for a site I'm developing. When I'm browsing posts, I can use Next/Previous links to walk between posts. I want to have the same thing on pages.

  • Page A
  • Page B
  • Page C

Page A should link to next sibling Page B. Page B should link to previous sibling Page A and next sibling Page C. Page C should link to previous sibling Page B.

Is there any plugin you can recommend that generates these links? I know there are some plugins that do this, but I specifically want one that hooks into my current theme automatically. I know how to edit the theme, but that would brick my site whenever a theme update is available.

I'm using the LightWord WordPress theme.

like image 269
Pieter Avatar asked Dec 26 '10 09:12

Pieter


3 Answers

Pop the following code into your functions.php file in your active theme directory:

function siblings($link) {
    global $post;
    $siblings = get_pages('child_of='.$post->post_parent.'&parent='.$post->post_parent);
    foreach ($siblings as $key=>$sibling){
        if ($post->ID == $sibling->ID){
            $ID = $key;
        }
    }
    $closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID));

    if ($link == 'before' || $link == 'after') { echo $closest[$link]; } else { return $closest; }
}

To call the function:

<?php siblings('before'); ?>

or

<?php siblings('after'); ?>

and it will echo out the link to the previous or next page.

If you want both you can leave the function empty and it will return an array with both links.

like image 90
jackreichert Avatar answered Nov 15 '22 20:11

jackreichert


from the wordpress Codex

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

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

<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="alignleft">
<a href="<?php echo get_permalink($prevID); ?>"
title="<?php echo get_the_title($prevID); ?>">Previous</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="alignright">
<a href="<?php echo get_permalink($nextID); ?>" 
 title="<?php echo get_the_title($nextID); ?>">Next</a>
 </div>
 <?php } ?>
</div><!-- .navigation -->
like image 40
fafchook Avatar answered Nov 15 '22 22:11

fafchook


A rough snippet of what I developed onwards from the answers here (to get a real link and the post title in action) - might prove helpful to someone!

$closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID));
$name =  array('before'=>get_the_title($siblings[$ID-1]->ID),'after'=>get_the_title($siblings[$ID+1]->ID));

if ($link == 'before' || $link == 'after') { 
echo '<a href="' . $closest[$link] . '">';
echo '<span>' . $name[$link] . '</span></a>';
}
else { return $closest; }
like image 2
Ville Niemi Avatar answered Nov 15 '22 22:11

Ville Niemi