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


当前第2页 返回上一页

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)方法的错误理解。


标签:WordPress

返回前面的内容

相关阅读 >>

wordpress 静态化设置 windows主机伪静态去除index.php方法

目前最流行的10款国外博客程序

详解黑客修改wordpress核心文件,劫持网站流量

wordpress免插件实现面包屑导航的示例代码

获得google pr值的php代码

wordpress使用中文用户名的方法

wordpress中使用php脚本来判断访客来自什么国家

wordpress 实现文章评论排行榜

wordpress中获取所使用的模板的页面id的简单方法

wordpress 显示文章摘要方法 详细篇

更多相关阅读请进入《wordpress》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...