get_previous_post()获取同分类上一篇文章

目录

描述

Retrieve previous post that is adjacent to current post.

get_previous_post() 描述

用法

<?php get_previous_post$in_same_term$excluded_categories$taxonomy ?>

get_previous_post() 用法

参数

$in_same_term

(boolean) (可选) Whether post should be in same category.

默认值: false

$excluded_categories

(string) (可选) Excluded categories IDs.

默认值: ''

$taxonomy

(string) (可选) Taxonomy, if $in_same_term is true. Added in WordPress 3.8.

默认值: 'category'

get_previous_post() 参数

返回值

  • Post object if successful.
  • Null if global $post is not set.
  • Empty string if no corresponding post exists.

get_previous_post() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?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; ?>

get_previous_post() 示例

注意

历史

添加于 版本: 1.5.0

get_previous_post() 历史

源文件

get_previous_post() 函数的代码位于 wp-includes/link-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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 );
}

get_previous_post() 源文件

相关