0
votes

i want to add next and previous page links within the same level.

I found a solution for all pages (child and parent) in the WordPress codex: https://codex.wordpress.org/Next_and_Previous_Links#The_Next_and_Previous_Pages

But what i want, is a navigation within the same level.

Any ideas how to solve this?

EDIT:

<?php
        $pagelist = get_pages('post_type=publikation&sort_column=menu_order&sort_order=asc');
        $pages = array();
        foreach ($pagelist as $page) {
           $pages[] += $page->ID;
        }

        $current = array_search(get_the_ID(), $pages);

        $testPost = (get_pages( array( 'post_type' => 'publikation' ,'child_of' => $post->ID ) ) || $post->post_parent);

        $prevID = $pages[$current-1];
        $nextID = $pages[$current+1];
        ?>

        <div class="navigation">
        <?php if (!empty($prevID) && !$testPost) { ?>
        <div class="alignleft">
        <a href="<?php echo get_permalink($prevID); ?>"
          title="<?php echo get_the_title($prevID); ?>">Previous</a>
        </div>
        <?php }
        if (!empty($nextID) && !$testPost) { ?>
        <div class="alignright">
        <a href="<?php echo get_permalink($nextID); ?>"
         title="<?php echo get_the_title($nextID); ?>">Next</a>
        </div>
        <?php } ?>
        </div><!-- .navigation -->`
2

2 Answers

1
votes

I know this topic is 5 years old, but I came across the same issue and found a easy solution. I'm gonna share it here in case someone is dealing with this problem too.

You just need to change get_pages(); into get_posts(); :

        $pagelist = get_posts('post_type=publikation&sort_column=menu_order&sort_order=asc');
0
votes

What about this...

Within the code you referenced:

<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
   $pages[] += $page->ID;
}

$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>

<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="alignleft">
<a href="<?php echo get_permalink($prevID); ?>"
  title="<?php echo get_the_title($prevID); ?>">Previous</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="alignright">
<a href="<?php echo get_permalink($nextID); ?>" 
 title="<?php echo get_the_title($nextID); ?>">Next</a>
</div>
<?php } ?>
</div><!-- .navigation -->

Edit this...

<?php if (!empty($prevID)) { ?>

... to check if the post is either a parent or a child of the current post.

<?php 

$testPost = (get_pages( array( 'child_of' => $post->ID ) ) || $post->post_parent);

if (!empty($prevID) && !$testPost) { ...

if (!empty($nextID) && !$testPost) { ...