Ask your WordPress questions! Pay money and get answers fast! (more info)

Next/Previous child page links

  • SOLVED

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

Answers (5)

2010-07-10

Darrin Boutote answers:

The default navigation typically used on single post pages (single.php) <em>will</em> 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>



Mark F. comments:

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.


Darrin Boutote comments:

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: [[LINK href="http://darrinb.com/about/the-services/"]]http://darrinb.com/about/the-services/[[/LINK]]



Darrin Boutote comments:

@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.


Mark F. comments:

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!

2010-07-10

Rashad Aliyev answers:

contact with me please..


Mark F. comments:

Thanks for trying!

2010-07-10

Nile Flores answers:

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/


Mark F. comments:

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.

2010-07-10

Ryan Imel answers:

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

2010-07-10

Pippin Williamson answers:

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 <em>only</em> on subpages.


Mark F. comments:

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!