WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How can I show links to previous/next Page by menu_order (7 posts)

  1. chrishajer
    Member
    Posted 5 years ago #

    I have a website where I have a handbook online, in WordPress Pages. Chapters are organized as parent Pages, then each parent might have 6 or 8 child Pages. I can list all the Pages using wp_list_pages and have it formatted as a sort of table of contents. This part works fine.

    I would like to be able to have the visitor browse the pages sequentially, rather than having to click the back button or use the menu navigation after viewing a Page. So, when they start reading at Chapter 1, and they want to go to child Page 1a, at the bottom of Page Chapter 1, there would be a link "Next" and possibly list the page title, then the link points to Page 1a.

    Let's say you're in the middle of the Pages (Chapter 5 for example): there would be a link to the previous Page (Chapter 4d maybe) and the next Page (Chapter 5a.) The next and previous pages to show as links would come from wp_lists_pages and use the menu order (rather than previous/next by date.)

    Any ideas?

    Thank you.

  2. djrrb8
    Member
    Posted 5 years ago #

    Hi Chris,

    Unfortunately I don't have a good solution; I'm actually in the same boat as you are in terms of wanting to cycle through my ordered WP Pages using next/previous links. I wanted to bump your thread in case there is anybody out there with a solution; one would think like this would be a pretty common thing to do.

    As a quick and dirty (and hopefully temporary) solution, I've started to store the next and previous post IDs in the custom fields. The downside is that I have to change these fields as well as the page order whenever I add a page.

    Thanks for posting this, and hopefully we can find some answers!

    Cheers,
    David

  3. Stephanie Leary
    Member
    Posted 5 years ago #

    I actually got this working -- sort of. Here's the code:

    <?php
    $posts = get_posts("post_type='page'&amp;post_status='publish'&amp;numberposts=99999&amp;orderby=menu_order");
    $pages = get_page_hierarchy($posts);
    //print_r($pages);
    $pages = array_keys($pages);
    
    $current = array_search($post->ID, $pages);
    $prevID = $pages[$current-1];
    $nextID = $pages[$current+1];
    
    /*
    echo "Current: $current";
    echo "Prev: $prevID";
    echo "Next: $nextID";
    */
    ?>
    
    <div class="navigation">
    <?php if (!empty($prevID)) { ?>
    <div class="alignleft"><a href="<?php echo get_permalink($prevID); ?>">Previous</a></div>
    <?php }
    if (!empty($nextID)) { ?>
    <div class="alignright"><a href="<?php echo get_permalink($nextID); ?>">Next</a></div>
    <?php } ?>
    <div class="clear"><!-- --></div>
    </div><!-- .navigation -->

    If you're using this on a page template, it can go inside or outside the loop.

    The commented print_r and echo lines are useful for seeing what's going on. (Wrap this whole chunk of code in pre tags if you do uncomment those lines.) The get_page_hierarchy function returns a flat array of pages with the children grouped after their parents. However, it seems to be sorting them in the reverse of my established menu_order. Not sure what to do about that, short of writing my own hierarchy function. I'm leaning toward doing just that, but... try this out and let me know what you think.

    The if statements surrounding the links ensure that the first page doesn't get a previous link, and the last page doesn't get a next.

    ETA: adding '&orderby=menu_order' to the get_posts parameters puts the top level pages in the correct order, but the children are still backwards.

    Also, this editor insists on encoding the ampersands in that parameter string... be sure to fix those before you try the code!

  4. Stephanie Leary
    Member
    Posted 5 years ago #

    Revised version that keeps things in order:

    <?php
    $pagelist = get_pages('sort_column=menu_order&amp;sort_order=asc');
    $pages = array();
    foreach ($pagelist as $page) {
       $pages[] += $page->ID;
    }
    
    //print_r($pages);
    
    $current = array_search($post->ID, $pages);
    $prevID = $pages[$current-1];
    $nextID = $pages[$current+1];
    
    /*
    echo "Current: $current";
    echo "Prev: $prevID";
    echo "Next: $nextID";
    */
    ?>
    
    <div class="navigation">
    <?php if (!empty($prevID)) { ?>
    <div class="alignleft"><a href="<?php echo get_permalink($prevID); ?>">Previous</a></div>
    <?php }
    if (!empty($nextID)) { ?>
    <div class="alignright"><a href="<?php echo get_permalink($nextID); ?>">Next</a></div>
    <?php } ?>
    <div class="clear"><!-- --></div>
    </div><!-- .navigation -->

    Test that for me? (Mind the ampersands.) If it works for you as well, I'll turn it into a plugin.

  5. chrishajer
    Member
    Posted 5 years ago #

    I'm sorry I didn't post back about this earlier. I was checking for replies for a little while after I posted it, but then gave up and didn't see your reply. Thank you for working it up.

    The prev and next links work exactly as expected, and the commented out echo commands display the proper page ID for the prev and next. For the current page, the array key is echoed, not the page ID. When you print_r the array, that key corresponds to the current page ID value. So, it's all good.

    I am going to try to make this into a plugin with a shortcode so my editors can use this easily without having to choose a specific template (which is where I hard-coded it.) Thanks once again.

    Chris

  6. Stephanie Leary
    Member
    Posted 5 years ago #

    I made it a plugin. Enjoy!

  7. chrishajer
    Member
    Posted 5 years ago #

    Thank you. Much better than what I was doing with it :-)

Topic Closed

This topic has been closed to new replies.

About this Topic