Support » Themes and Templates » Need to add previous / next page functionality, with some fine tuning

  • I need to create previous/next type links on sibling pages. Basically something similar to this http://wordpress.org/support/topic/next-page-literally?replies=23 (the version posted by kristjan)

    The code I’ve been trying thus far is a bit different. In functions.php I’ve got:

    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; }
    }

    And then in my sidebar I have:
    <a href="<?php siblings('before'); ?>">Previous</a> <a href="<?php siblings('after'); ?>">Next</a>

    This method has a problem – I haven’t found a way to make it so that when one is on the last page in the list, that the Next link either goes away or if I could even add a class to it so I can modify it in CSS.

    Question 1: How can I make it so that the Previous link will either be gone or I can add a class to it if it’s on the Previous page? (And obviously, the same with Next.

    The second question is a bit more tricky. I need to add a page number between Previous & Next. So it would look like, for example, Previous 5/32 Next. Again, this is only with the sibling pages.

    Question 2: How do I add page number including the total number of sibling pages?

    Thanks much

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    just a rough idea:

    1)
    check if $ID is equal 0 zero; or $ID is equal count($siblings), resp.

    2)
    total number should be count($siblings);
    the page you are on should be $ID (i am not sure here, if you have to add 1 because the keys start with 0)

    Thanks. Think you could post a slightly more complete version of that so it’s easier for me to work from?

    I found this http://wpquestions.com/question/show/id/614 and that solves my question #1.. Still working on 2…

    Michael

    (@alchymyth)

    Forum Moderator

    this is just a suggestion; not much commented:

    http://pastebin.com/KkdwLc6z

    (likely to be totally different from what you found in the ‘wpquestions’ post; certainly somehow different from your first approach.)

    Wow, HUGE THANKS! that really helped. One tweak though… how can I get the sort order not to be the page ID but to be the menu order (the order value that can be set in the admin)?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Need to add previous / next page functionality, with some fine tuning’ is closed to new replies.