wordpress中is_sticky()判断文章是否置顶的参数与用法

   2015-08-01 0
核心提示:这篇文章主要介绍了wordpress中is_sticky()判断文章是否置顶的参数与用法,需要的朋友可以参考下

【说明】

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

【用法】


复制代码
代码如下:

<?php is_sticky($post_ID); ?>

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

【示例】


复制代码
代码如下:

is_sticky();
// 任意置顶文章被显示.</p> <p>is_sticky('17');
// 当ID为17的文章被显示.

【源文件】

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

PHP Code复制内容到剪贴板
  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. }  


这里要举例说明的是:

is_sticky(10) 是判断 $post_id为 10的文章是否是置顶文章,而不是说所有置顶文章中post_id为 10的置顶文章。之所以会有后者错误的理解,也是自己看了官方对于 is_sticky($post_id)方法用法文档比较模糊的介绍,其实细究起来,“所有置顶文章中post_id为 10的置顶文章” 这种判断也是多余的,直接 $post->id==10 或 get_the_id()==10 判断当前文章$post_id是否等于10 就好了!


这里还得感谢下友链中的tiandi兄在本站中留言中提醒说不存在“is_sticky($post_ID)中参数失效”的问题,指正自己对wordpress is_sticky($post_id)方法的错误理解。

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

点击排行