wordpress检查文章是否置顶函数:is_sticky()


【说明】

检查当前文章是否置顶。返回值TRUE 或者 FALSE.

【用法】

  1. <?php is_sticky($post_ID); ?>

【参数】
$post_ID
(string) (optional) 文章 ID
默认: None
返回值
(boolean)
True,或 false.

【示例】

  1.  
  2. is_sticky();
  3. // 任意置顶文章被显示.
  4.  
  5. is_sticky('17');
  6. // 当ID为17的文章被显示.

【源文件】

is_sticky() 位于 wp-includes/post.php.

  1. /**
  2. * Check if post is sticky.
  3. *
  4. * Sticky posts should remain at the top of The Loop. If the post ID is not
  5. * given, then The Loop ID for the current post will be used.
  6. *
  7. * @since 2.7.0
  8. *
  9. * @param int $post_id Optional. Post ID.
  10. * @return bool Whether post is sticky.
  11. */
  12. function is_sticky( $post_id = 0 ) {
  13. $post_id = absint( $post_id );
  14.  
  15. if ( ! $post_id )
  16. $post_id = get_the_ID();
  17.  
  18. $stickies = get_option( 'sticky_posts' );
  19.  
  20. if ( ! is_array( $stickies ) )
  21. return false;
  22.  
  23. if ( in_array( $post_id, $stickies ) )
  24. return true;
  25.  
  26. return false;
  27. }