Need to add previous / next page functionality, with some fine tuning (6 posts)

  1. MAVIC
    Posted 5 years ago #

    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

  2. Michael
    Forum Moderator
    Posted 5 years ago #

    just a rough idea:

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

    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)

  3. MAVIC
    Posted 5 years ago #

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

  4. MAVIC
    Posted 5 years ago #

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

  5. Michael
    Forum Moderator
    Posted 5 years ago #

    this is just a suggestion; not much commented:


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

  6. MAVIC
    Posted 5 years ago #

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


Topic Closed

This topic has been closed to new replies.

About this Topic