您可以在两个ifs中添加一个else,并获得第一篇帖子/最新帖子:
<?php
$nextPost = get_next_post();
if($nextPost) {
$nextPostID = $nextPost->ID;
?>
<a class="prev-post" href="<?php echo get_permalink( $nextPostID ); ?>">
<?php echo $nextPost->post_title; ?>
</a>
<?php } else {
$first_post = get_posts( array(
\'posts_per_page\' => 1,
\'order\' => \'ASC\'
) );
?>
<a class="prev-post" href="<?php echo get_permalink( $first_post[0]->ID ); ?>">
<?php echo get_the_title( $first_post[0]->ID ); ?>
</a>
<?php } ?>
<?php
$prevPost = get_previous_post();
if($prevPost) {
$prevPostID = $prevPost->ID;
?>
<a class="next-post" href="<?php echo get_permalink( $prevPostID ); ?>">
<?php echo $prevPost->post_title; ?>
</a>
<?php } else {
$latest_post = get_posts( array(
\'posts_per_page\' => 1,
\'order\' => \'DESC\'
) );
?>
<a class="next-post" href="<?php echo get_permalink( $latest_post[0]->ID ); ?>">
<?php echo get_the_title( $latest_post[0]->ID ); ?>
</a>
<?php } ?>
我假设您只想将其用于帖子,所以在get\\u posts上,我将默认的帖子类型保留为post。