Next/Prev links for posts with same parent in word

2019-06-09 06:22发布

How to create previous/next links for posts with the same parent post (not category)?

similar to this:

$previous_post = get_adjacent_post( true, '', true);

but for posts with the same parent.

标签: wordpress
1条回答
劳资没心,怎么记你
2楼-- · 2019-06-09 07:00

I presume your'e talking about pages.

so you would need to declare the parent then use get_pages to call the other pages.

so in your loop:

<?php $parent = $post->post_parent; $pagelist = get_pages('post_type=page&sort_column=menu_order&sort_order=desc&child_of='.$parent); $pages = array(); foreach ($pagelist as $page) {$pages[] += $page->ID;}
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<?php if (!empty($prevID)) { ?>
<a href="<?php echo get_permalink($prevID); ?>" title="<?php echo get_the_title($prevID); ?>"><?php echo get_the_title($prevID); ?></a>
<?php }
if (!empty($nextID)) { ?>
<a href="<?php echo get_permalink($nextID); ?>" title="<?php echo get_the_title($nextID); ?>"><?php echo get_the_title($nextID); ?></a>
<?php } ?>
查看更多
登录 后发表回答