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主题支持自定义菜单及修改css样式实现方法

wordpress高级自定义布局的内容编辑器(tinymce)模板

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

centos下搭建php环境与wordpress博客程序的全流程总结

wordpress菜单css类选项设置方法

wordpress站点出现404错误时邮件通知管理员的方法

wordpress无法登录后台的解决方案

wordpress 如何从后台数据库修改theme(图文教程)

在coreos上搭建一个wordpress程序操作实例

基于wordpress的ajax写法详解

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



打赏

取消

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

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

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

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

评论

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