首页 > CMS建站 > Wordpress > 正文

wordpress中is_sticky()置顶文章方法参数与用法

2020-06-10 12:56:59
字体:
来源:转载
供稿:网友

wordpress is_sticky($post_id) 不带参数时,主要用来在循环输出文章列表中时,用来判断当前$post_id是否是置顶文章,带参数则是判断给定的$post_id是否是置顶文章.

说明:检查当前文章是否置顶,返回值TRUE 或者 FALSE.

用法:<?php is_sticky($post_ID); ?>

参数:$post_ID

(string) (optional) 文章 ID

默认:None

返回值:(boolean) True,或 false.

示例:

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

源文件:

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

这里要举例说明的是:

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选