logo

$18
Next/Previous child page links

At the bottom of a child page I would like it to have a link (using the title of the respective page) to the next and previous child page. Ordered by date preferably.

For example, I have my pages like this:

Parent Page
-Child1
-Child2
-Child3

When I'm on Child2 at the bottom of the page i want these links to show up:

<<Child 1 Child3>>

I am currently using the Thematic framework.

If there is any code that you need posted or have any questions please let me know.

Thank you,
MDF

Mark F. | 07/10/10 at 4:29pm | Edit


(5) Possible Answers Submitted...

  • avatar
    Last edited:
    07/10/10
    4:33pm
    Rashad Aliyev says:

    contact with me please..

    • 07/10/10 6:45pm

      Mark F. says:

      Thanks for trying!

  • avatar
    Last edited:
    07/11/10
    1:41am
    Nile Flores says:

    I have never seen this for pages, but for posts.

    However, you might try taking a look at the arrays available for child_of and what you might be able to do, and put a template specifically for those pages

    http://codex.wordpress.org/Function_Reference/wp_list_pages

    As a side note- have you looked at the following plugin and tried it out... would it work for you?

    http://wordpress.org/extend/plugins/next-page/

    • 07/11/10 3:23pm

      Mark F. says:

      I am currently using that plugin as a work around, however it does not contain the links to JUST child pages which is what I was really hoping for.

  • avatar
    Last edited:
    07/12/10
    10:14pm
    Darrin Boutote says:

    The default navigation typically used on single post pages (single.php) will work on a Page (I just threw it on the bottom of my page.php template to test), but it might not work exactly the way you want it to:

    <div class="navigation">
    <div class="alignleft"><?php previous_post_link('&laquo; %link') ?></div>
    <div class="alignright"><?php next_post_link('%link &raquo;') ?></div>
    </div>


    • 07/11/10 3:24pm

      Mark F. says:

      I'm really looking for a way to isolate it to the child pages. I will give this code a try in a bit and see what it does differently than what I am currently using as a work around (the next page plugin). Thank you.

    • 07/12/10 12:59pm

      Darrin Boutote says:

      Reinvented the wheel a bit here, but this should work for you. Just add it to your functions.php file:


      <?php
      // function to find location within array
      function relative_value_array($array, $current_val = '', $offset = 1) {
      $values = array_values($array);
      $current_val_index = array_search($current_val, $values);

      if( isset($values[$current_val_index + $offset]) ) {
      return $values[$current_val_index + $offset];
      }
      return false;
      };

      // previous page link function
      function dbdb_prev_page_link() {
      global $post;

      if ( isset($post->post_parent) && $post->post_parent > 0 ) {
      $children = get_pages('&sort_column=post_date&sort_order=asc&child_of='.$post->post_parent.'&parent='.$post->post_parent);
      };

      // throw the children ids into an array
      foreach( $children as $child ) { $child_id_array[] = $child->ID; }

      $prev_page_id = relative_value_array($child_id_array, $post->ID, -1);

      $output = '';
      if( '' != $prev_page_id ) {
      $output .= '<a href="' . get_page_link($prev_page_id) . '"> &laquo; '. get_the_title($prev_page_id) . '</a>';
      }
      return $output;
      };

      //next page link function
      function dbdb_next_page_link() {
      global $post;

      if ( isset($post->post_parent) && $post->post_parent > 0 ) {
      $children = get_pages('&sort_column=post_date&sort_order=asc&child_of='.$post->post_parent.'&parent='.$post->post_parent);
      };

      // throw the children ids into an array
      foreach( $children as $child ) { $child_id_array[] = $child->ID; }

      $next_page_id = relative_value_array($child_id_array, $post->ID, 1);

      $output = '';
      if( '' != $next_page_id ) {
      $output .= '<a href="' . get_page_link($next_page_id) . '">'. get_the_title($next_page_id) . ' &raquo;</a>';
      }
      return $output;
      };
      ?>


      And add this to your page template:


      <div class="navigation">
      <div class="alignleft"><?php echo dbdb_prev_page_link(); ?></div>
      <div class="alignright"><?php echo dbdb_next_page_link(); ?></div>
      </div>


      You can see a working example here: http://darrinb.com/about/the-services/


    • 07/12/10 1:07pm

      Darrin Boutote says:

      @pippin: The issue with the script you provided is that a link to a page outside of the sub-page's parent hierarchy will show up. For example if your page structure is like so:

      Home
      About
      - Author
      - Site
      - Misc

      a link to Home or About will show up when you're on the Author page b/c the script you provided is only testing to see whether or not the navigation should appear, not what the navigation should point to, and Mark wants navigation only among the child pages of a particular parent.

    • 07/12/10 10:12pm

      Mark F. says:

      You NAILED it. Thank you so much! It seems like it was a bit of a tall order. I figured it was something simple and that I was just missing it!

  • avatar
    Last edited:
    07/11/10
    2:37pm
    Ryan Imel says:

    I ran into this on a project not long ago too. The solution I used was actually a Plugin which does just what you're describing. It's called Next Page, not Next Post:

    http://wordpress.org/extend/plugins/next-page-not-next-post/

    I'd post code, but it would probably be easier to grab it from there and implement it into your site via a Plugin anyway :)

  • avatar
    Last edited:
    07/12/10
    11:17am
    Pippin Williamson says:

    First create an is_subpage function in your functions.php file:


    function is_subpage()
    {
    global $post, $wpdb;

    if ( is_page() AND isset( $post->post_parent ) != 0 )
    {
    $aParent = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d AND post_type = 'page' LIMIT 1", $post->post_parent ) );
    if ( $aParent->ID ) return true; else return false;
    }
    else
    {
    return false;
    }
    }


    Then run the test on your navigation:


    <?php if(is_subpage())
    { ?>
    <div class="navigation">

    <div class="alignleft"><?php previous_post_link('&laquo; %link') ?></div>

    <div class="alignright"><?php next_post_link('%link &raquo;') ?></div>

    </div>
    <?php } ?>


    This will show the navigation only on subpages.

    Previous versions of this answer: 07/12/10 at 11:17am

    • 07/12/10 10:11pm

      Mark F. says:

      Thanks for trying Pippen, although your code kind of meets what I'm asking for (which I guess was a tall order) it doesn't stay contained to the child pages which is what I was really hoping for. Darrin above got it. Take the code and give it a whirl! Not to worry though, I'm sure that I'll be back with more questions!

This question has expired.





Current status of this question: Completed