描述:
获取同分类下的上一篇文章
用法:
<?php get_previous_post( $in_same_term, $excluded_categories, $taxonomy ) ?>
参数:
$in_same_term
(boolean) (可选) 是否属于同一分类
默认值: false
$excluded_categories
(string) (可选) 要排除的分类ID
默认值: ''
$taxonomy
(string) (可选) 分类法,如果$in_same_term为true。在WordPress3.8中添加。
默认值: 'category'
示例:
<?php
$prev_post = get_previous_post();
if (!empty( $prev_post )): ?>
<a href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo $prev_post->post_title; ?></a>
<?php endif; ?>
源文件:
/**
* Retrieve previous post that is adjacent to current post.
*
* @since 1.5.0
*
* @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
* @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
* @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
* @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
*/
function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
}