Archived topic
Restrict pagination to children of a particular page
9 replies · Started by George on September 2, 2021
I have set a pagination template block element to only appear below children of a particular page:
add_filter( 'generate_block_element_display', function( $display, $element_id ) {
global $post;
if ( 11024234 === $element_id && ( is_page() && $post->post_parent == '11017715' ) ) {
$display = true;
}
return $display;
}, 10, 2 );
Is it possible to restrict the pagination only to those child pages?
Hi George,
Can you try this?
add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
$args = array( 'child_of' => 11017715, );
$pages = get_pages($args);
$pageID = array();
foreach($pages as $page){
$pageID[] = $page->ID;
}
if ( 11024234 === $element_id && is_page($pageID) ) {
$display = true;
}
return $display;
}, 20, 2 );
Thanks, Elvin, just tested it, it still includes some other random pages.
Hi George,
First off - does it work on the child pages it is meant to ?
Yes, it does, just like the code, I showed in my initial post. It's just that navigating with previous, next will eventually show a page that is not a child of the stated parent.
Hmmm... i am not sure theres a solution for that, to best of my knowledge GP is using the get_next_post_link ( and get_prev ) functions for those links:
https://developer.wordpress.org/reference/functions/get_next_post_link/
Which doesn't have any options in regards to page hierarchy.
I think you would need to create you won paging nav. Heres an example function that sounds like its doing the same thing:
I managed to make it work with the code you provided, David. The only thing is that when I use gp130428_paginate_parent_children( 123 ) the pagination also includes the parent page. Is there a way to exclude the parent?
Line 37 looks to be where it outputs the Page parent title. Try replacing that line that with just a HTML Comment eg.
<!-- does nothing -->
See what happens :)
Yeah, I tried all that stuff, it didn't work. Never mind, I will play some more with it and see what happens.
Thanks, David 🙂
No problems - hope you find a solution!