Site logo

[Resolved] Restrict pagination to children of a particular page

Home Forums Support [Resolved] Restrict pagination to children of a particular page

Home Forums Support Restrict pagination to children of a particular page

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1917051
    George

    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?

    #1917194
    Elvin
    Staff
    Customer Support

    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 );
    #1917634
    George

    Thanks, Elvin, just tested it, it still includes some other random pages.

    #1917671
    David
    Staff
    Customer Support

    Hi George,

    First off – does it work on the child pages it is meant to ?

    #1917703
    George

    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.

    #1917936
    David
    Staff
    Customer Support

    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:

    https://gist.github.com/glueckpress/5477286

    #1918010
    George

    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?

    #1918501
    David
    Staff
    Customer Support

    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 🙂

    #1918559
    George

    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 🙂

    #1919298
    David
    Staff
    Customer Support

    No problems – hope you find a solution!

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.