2 years ago
#44217

Christopher Costa
Wordpress sibling page navigation breaks footer on pages that don't have siblings
I'm trying to edit my theme (child theme of twentytwentyone) to add page navigation between siblings, but I don't want show the navigation on pages that do not have siblings.
I've found a bit of code that is working for me for the most part, but something about it makes the page stop short and not show footers on pages that don't have siblings.
In my child theme's functions.php I have this code
<?php // function to find location within array
function relative_value_array($array, $current_val = '', $offset = 1) {
$values = array_values($array);
$current_val_index = array_search($current_val, $values);
if( isset($values[$current_val_index + $offset]) ) {
return $values[$current_val_index + $offset];
}
return false;
};
// previous page link function
function dbdb_prev_page_link() {
global $post;
if ( isset($post->post_parent) && $post->post_parent > 0 ) {
$children = get_pages('&sort_column=post_date&sort_order=asc&child_of='.$post->post_parent.'&parent='.$post->post_parent);
};
// throw the children ids into an array
foreach( $children as $child ) { $child_id_array[] = $child->ID; }
$prev_page_id = relative_value_array($child_id_array, $post->ID, -1);
$output = '';
if( '' != $prev_page_id ) {
$output .= '<a href="' . get_page_link($prev_page_id) . '"> « '. get_the_title($prev_page_id) . '</a>';
}
return $output;
};
//next page link function
function dbdb_next_page_link() {
global $post;
if ( isset($post->post_parent) && $post->post_parent > 0 ) {
$children = get_pages('&sort_column=post_date&sort_order=asc&child_of='.$post->post_parent.'&parent='.$post->post_parent);
};
// throw the children ids into an array
foreach( $children as $child ) { $child_id_array[] = $child->ID; }
$next_page_id = relative_value_array($child_id_array, $post->ID, 1);
$output = '';
if( '' != $next_page_id ) {
$output .= '<a href="' . get_page_link($next_page_id) . '">'. get_the_title($next_page_id) . ' »</a>';
}
return $output;}
and I've added the actual navigation links to my theme's content-page.php
<div class="prevnext alignwide">
<div class="alignleft"><?php echo dbdb_prev_page_link(); ?></div>
<div class="alignright"><?php echo dbdb_next_page_link(); ?>/div>
</div>
I believe that something in the functions (perhaps an if statement?) is causing the loop to break before it displays the footer on the bottom of my pages, and it also breaks the Nav menu at the top making it not show subpages/children but I can't figure it out. I would really appreciate any help with this. The page I'm working on is https://voicefromthevoid.com/staging/
php
wordpress
navigation
children
siblings
0 Answers
Your Answer