在WordPress中,可以使用get_previous_post()
和get_next_post()
函数来获取上一篇和下一篇文章。
要显示上一篇文章,可以使用以下代码:
php复制代码<?php $prev_post = get_previous_post();if( ! empty( $prev_post ) ):$prev_permalink = get_permalink( $prev_post->ID );$prev_title = apply_filters('the_title', $prev_post->post_title );echo '<a href="' . $prev_permalink . '">' . $prev_title . '</a>';else:echo "没有了,已经是第一篇文章";endif;?>
要显示下一篇文章,可以使用以下代码:
php复制代码<?php $next_post = get_next_post();if( ! empty( $next_post ) ):$next_permalink = get_permalink( $next_post->ID );$next_title = apply_filters('the_title', $next_post->post_title );echo '<a href="' . $next_permalink . '">' . $next_title . '</a>';else:echo "没有了,已经是最后一篇文章";endif;?>
这些代码会检查是否存在上一篇或下一篇文章,如果存在,则获取文章的链接和标题,并将其显示为链接。如果没有上一篇或下一篇文章,则会显示一条消息提示。