Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

wordpress - Browsing page siblings through next/previous links

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...