Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active May 22, 2020 13:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/9273447 to your computer and use it in GitHub Desktop.
Save cferdinandi/9273447 to your computer and use it in GitHub Desktop.
Two simple functions for getting the ID of next and previous pages (not posts) in Wordpress. Forked from the "Next Page, Not Next Post" plugin by Matt McInvale. http://wordpress.org/plugins/next-page-not-next-post/

Functions

Next Page: next_page_ID($id) Previous Page: previous_page_ID($id)

$id: The ID of the page you're trying to get the next or previous page for.

Examples

$next_ID = next_page_ID('41);
$previous_ID = previous_page_ID('41);
$next_url = get_permalink($next_ID);

echo '<a href="' . $next_url . '">Next Page</a>';
<?php
/* =============================================================
NEXT PAGE ID
A function to get the next page's ID.
$id = ID of the page you want to find the next page for.
* ============================================================= */
function next_page_ID($id) {
// Get all pages under this section
$post = get_post($id);
$post_parent = $post->post_parent;
$get_pages_query = 'child_of=' . $post_parent . '&parent=' . $post_parent . '&sort_column=menu_order&sort_order=asc';
$get_pages = get_pages($get_pages_query);
$next_page_id = '';
// Count results
$page_count = count($get_pages);
for ($p=0; $p < $page_count; $p++) {
// Get the array key for our entry
if ($id == $get_pages[$p]->ID) break;
}
// Assign our next key
$next_key = $p+1;
// If there isn't a value assigned for the previous key, go all the way to the end
if (isset($get_pages[$next_key])) {
$next_page_id = $get_pages[$next_key]->ID;
}
return $next_page_id;
}
/* =============================================================
PREVIOUS PAGE ID
A function to get the previous page's ID.
$id = ID of the page you want to find the previous page for.
* ============================================================= */
function previous_page_ID($id) {
// Get all pages under this section
$post = get_post($id);
$post_parent = $post->post_parent;
$get_pages_query = 'child_of=' . $post_parent . '&parent=' . $post_parent . '&sort_column=menu_order&sort_order=asc';
$get_pages = get_pages($get_pages_query);
$prev_page_id = '';
// Count results
$page_count = count($get_pages);
for($p=0; $p < $page_count; $p++) {
// get the array key for our entry
if ($id == $get_pages[$p]->ID) break;
}
// assign our next & previous keys
$prev_key = $p-1;
$last_key = $page_count-1;
// if there isn't a value assigned for the previous key, go all the way to the end
if (isset($get_pages[$prev_key])) {
$prev_page_id = $get_pages[$prev_key]->ID;
}
return $prev_page_id;
}
?>
@underdoeg
Copy link

Thanks for this. This is a modified version that shows the next parent page when the last child is reached.

function nextPageId($id, $postType) {
	// Get all pages under this section
	$post = get_post($id);
	$post_parent = $post->post_parent;
	$get_pages_query = 'post_type='.$postType.'&child_of=' . $post_parent . '&parent=' . $post_parent . '&sort_column=menu_order&sort_order=asc';
	$pages = get_pages($get_pages_query);

	$breakNext = false;
	foreach($pages as $p){
		if($breakNext) return $p->ID;
		if($p->ID == $id){
			$breakNext = true;
		}
	}

	// looks like it was the last child of this parent, get the next parent
	if($post_parent > 0){
		return nextPageId($post_parent, $postType);
	}

	return -1;
}

@NomadPS
Copy link

NomadPS commented May 22, 2020

Hi could you please help me. I have tried to use a code to do next and previous with shortcodes in function but the ID order of page is not followed...
Could you please tell me how to

  1. amend my code to use it with ID order of same category
    or
  2. Use your code but with image and category
    Thank you so much for your help. I think it because I used Elementor in wordpress that it messed things up :(

`
add_shortcode( 'prev', 'prev_shortcode' );
add_shortcode( 'next', 'next_shortcode' );
function next_shortcode($atts) {
global $post;
ob_start();
next_post_link( '

%link
', '

', '%title ' . _x( '→', 'Previous post link', 'morphology' ) . '',
true);
$result = ob_get_contents();
ob_end_clean();
return $result;
}

function prev_shortcode($atts) {
global $post;
ob_start();
previous_post_link( '

%link
', '

', '%title ' . _x( '→', 'Previous post link', 'morphology' ) . '',
true );
$result = ob_get_contents();
ob_end_clean();
return $result;
}``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment