Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

pagination to next page with the same parent WordPress

  • SOLVED

i need a function to go to the next page (page, not post) which have the same parent
and a function to go to the previous page which have the same parent

I will use this function in page.php

Answers (3)

2011-06-04

Ivaylo Draganov answers:

Hello,

you can use <em>adjacent_post_link</em> function to print the links to the previous/next page by date:

adjacent_post_link('%link', '%title'); // the previous page
adjacent_post_link('%link', '%title', false, '', false); // the next page


I've not yet tested how that behaves with subpages. I'll make a test and come back later.


jevusi comments:

i want display only the pages whiche have the same parent.


Ivaylo Draganov comments:

Yes, I understand your goal. I am currently working on that :)


Ivaylo Draganov comments:

I think I've cracked it. Put this in your <em>functions.php</em>:

// function to filter the where clause in the query for adjacent posts
function sql_get_adjacent_post_where( $previous = true ) {

global $post, $wpdb;

// set SQL comparison operator
$op = $previous ? '<' : '>';

// build and return where clause
return $wpdb->prepare("WHERE p.post_date {$op} %s AND p.post_type = %s AND p.post_status = 'publish' AND p.post_parent = %s", $post->post_date, $post->post_type, $post->post_parent);

}

// previoust post where clause
function sql_get_previous_post_where() {
return sql_get_adjacent_post_where();
}

// next post where clause
function sql_get_next_post_where() {
return sql_get_adjacent_post_where( false );
}


And the use this to print the links in your <em>page.php</em>:

// filter queries
add_filter( 'get_previous_post_where', 'sql_get_previous_post_where', $in_same_cat, $excluded_categories );
add_filter( 'get_next_post_where', 'sql_get_next_post_where', $in_same_cat, $excluded_categories );

// print links
adjacent_post_link('%link', '%title'); // preious page
adjacent_post_link('%link', '%title', false, '', false); // next page


Works for me with subpages of the main page. It could be wrapped in a single template function but I find that a bit overkill. Let me know if you manage to apply it.


jevusi comments:

Ivaylo, you use get_previous_post_where but here http://adambrown.info/p/wp_hooks/hook/get_previous_post_where i can see "Warning! This hook has been renamed!"
Is is not a problem ?


Ivaylo Draganov comments:

Yes, it is now a variable hook:
[[LINK href="http://mobile.adambrown.info/p/wp_hooks/hook/get_%7B$adjacent%7D_post_where"]]http://mobile.adambrown.info/p/wp_hooks/hook/get_%7B$adjacent%7D_post_where[[/LINK]]


get_{$adjacent}_post_where


It refers both to <em>get_next_post_where</em> and <em>get_previous_post_where</em>

I will try to see whether I can optimize that code down to a single function for applying the filters. But if you like, you can use it like that as well.


Ivaylo Draganov comments:

Seems like filters can't be applied inside of the same function, so you should use the code like it is. Just change the filters to avoid a PHP Notice for undefined variables:

add_filter( 'get_previous_post_where', 'sql_get_previous_post_where', false, '' );
add_filter( 'get_next_post_where', 'sql_get_next_post_where', false, '' );


I hope this is the result that you were after. Cheers :-)


Ivaylo Draganov comments:

You can change the way that links appear with by passing parameters to the function [[LINK href="http://hitchhackerguide.com/2011/02/11/adjacent_post_link/"]]adjacent_post_link[[/LINK]]. There's no codex page for it, but the parameters are the same as for [[LINK href="http://codex.wordpress.org/Function_Reference/next_post_link"]]next_post_link[[/LINK]].

For example this gives you a link with the page title as its text:

adjacent_post_link('%link', '%title');


And this will just read "Previous page":

adjacent_post_link('%link', 'Previous page');


Appending/prepending text is also possible:

adjacent_post_link('Previous page: %link', '%title');

2011-06-04

Ryan Riatno answers:

Try to use get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; in your page.php


Ryan Riatno comments:


<?php $temp_query = $wp_query; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
'caller_get_posts'=>1,
'category_name' => 'Tricks',
'post__not_in' => $sticky,
'paged'=>$paged,
'posts_per_page'=>6,
);
//query_posts($args);
$my_query = new WP_Query($args);

while ($my_query->have_posts()) : $my_query->the_post();
?>


jevusi comments:

is it a response to my question ?


Ryan Riatno comments:

You just use <!--nextpage--> in post editor


Ryan Riatno comments:

Oh sorry, I think I misunderstood your questions. Will try to solve the problem


Ryan Riatno comments:

Is this what you [[LINK href="http://codex.wordpress.org/Function_Reference/posts_nav_link"]]mean [[/LINK]] ?


jevusi comments:

no


Ryan Riatno comments:

Maybe this [[LINK href="http://wordpress.org/extend/plugins/next-page-not-next-post/"]]plugin [[/LINK]]would help you

2011-06-07

Fahad Murtaza answers:

Are you still looking for an answer? If so , I can provide.