|
<?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; |
|
} |
|
|
|
?> |
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
or
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( '

', '%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( '

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