WordPress Next Parent Page Link

This is a self Q&A.

Often in WordPress you use the page hierarchy to build out project structure. For example, when doing portfolio sites, this is common:

Read More
  • Work
    • BMW
      • Models
        • 3-series
        • 5-series
    • Audi
      • Models
        • A3
        • A4

So, when the user is on the 3-series page, often you’d like to have a link to the “Next car manufacturer”. How can you do that without a plugin?

Related posts

Leave a Reply

1 comment

  1. These functions allow you to set the depth you want to use to determine the next page. So in the question, the user was on ‘3-series’, so the depth would be 2. So, the link returned would be to the “Audi” page.

    It’s used like this in your template (my example is using an image for the link text):

    $nextIMG = '<img src="'.get_template_directory_uri().'/images/icon-nav-right.png"/>';            
    echo next_project_link($nextIMG); 
    

    And place this in functions.php:

     /*
     * Next Project Link
     */
        function next_project_link($html) {
            global $post;
    
            // Change this to set what depth you want the next page of
            $parent_depth = 2;
    
            $ancestors = get_post_ancestors($post);     
            $current_project_id = $ancestors[$parent_depth-1];
    
            // Check for cached $pages
            $pages = get_transient( 'all_pages' );
            if ( empty( $transient ) ){
                $args = array(
                    'post_type'         => 'page',
                    'order'             => 'ASC',
                    'orderby'           => 'menu_order',
                    'post_parent'       => $ancestors[$parent_depth],
                    'fields'            => 'ids',
                    'posts_per_page'    => -1
                );
                $pages = get_posts($args);   
                set_transient('all_pages', $pages, 10 );
            }       
    
            $current_key = array_search($current_project_id, $pages);
            $next_page_id = $pages[$current_key+1];
    
            if( isset($next_page_id) ) {
                // Next page exists
                return '<a class="next-project" href="'.get_permalink($next_page_id).'">'.$html.'</a>';
            }
    
        } 
    
    
    /*
     * Previous Project Link
     */
        function previous_project_link($html) {
            global $post;
    
            // Change this to set what depth you want the next page of
            $parent_depth = 2;
    
            $ancestors = get_post_ancestors($post);
            $current_project_id = $ancestors[$parent_depth-1];
    
            // Check for cached $pages
            $pages = get_transient( 'all_pages' );
            if ( empty( $transient ) ){
                $args = array(
                    'post_type'         => 'page',
                    'order'             => 'ASC',
                    'orderby'           => 'menu_order',
                    'post_parent'       => $ancestors[$parent_depth],
                    'fields'            => 'ids',
                    'posts_per_page'    => -1
                );
                $pages = get_posts($args);   
                set_transient('all_pages', $pages, 10 );
            }       
    
            $current_key = array_search($current_project_id, $pages);
            $prev_page_id = $pages[$current_key-1];
    
            if( isset($prev_page_id) ) {
                // Previous page exists
                return '<a class="previous-project" href="'.get_permalink($prev_page_id).'">'.$html.'</a>';
            }
    
        }