Support » Fixing WordPress » Custom Page Links

  • Resolved brad8985


    I have a page where I list all child pages. That works fine.

    When I click a link and am taken to a page I would like to have a prev / next link to the the prev and next pages that were listed on the initial page. These are NOT posts.

    How would I go about that. I thought I could just add “1” to the current pages ID but the ID’s are all over the show and randomly numbered it seems.

Viewing 6 replies - 1 through 6 (of 6 total)
  • This is a little convoluted, and there may be an easier answer. I am assuming that you use wp_list_pages() to list the child pages.

    In the loop in the child template, use a call to wp_list_pages similar to this:

    $pagelist = wp_list_pages("title_li=&echo=0&child_of=$post->post_parent");

    After the loop, explode $pagelist on ‘<li ‘. Search the resulting array for the current ‘page_id=nn’. Use the entry before for the previous page, and the entry after for the next page.

    Thanks I will give that a go… not too sure what this piece means though… ‘page_id=nn’

    You will need to find the entry that corresponds to the current page by searching for ‘page_id=nn’ where ‘nn’ is the current page_id.

    Cant figure out how to do this are you able to give me an example?

    These lines go in the Loop to save the page ID and the child page list:

    $current_page = $post->ID;
    $pagelist = wp_list_pages("title_li=&child_of=$post->post_parent&echo=0");

    Here is a sample of how you could use the saved values after the Loop:

    $pagearray = explode('<li ',$pagelist);
    $junk = array_shift($pagearray); // First entry is empty
    for ($i=0;$i<sizeof($pagearray);++$i) {
       if (strpos($pagearray[$i],"$current_page")) {
    if ($i < sizeof($pagearray)) {
       echo '<p>';
       // $i points to current page entry in $pagearray
       if ($i > 0) {  // Is there a previous entry?
         $prevlink = $pagearray[$i - 1];
         $prevlink = preg_replace('/^.+<a/','<a',$prevlink);
         $prevlink = preg_replace('/<\/li>$/','',$prevlink);
         echo "&laquo; $prevlink";
       if ($i < (sizeof($pagearray) - 1)) {  // Is there a next entry?
         $nextlink = $pagearray[$i + 1];
         $nextlink = preg_replace('/^.+<a/','<a',$nextlink);
         $nextlink = preg_replace('/<\/li>$/','',$nextlink);
         echo "&nbsp&nbsp&nbsp $nextlink &raquo;";
       echo '</p>';

    Thanks that worked great 😉

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Page Links’ is closed to new replies.