|
wordpress is_sticky($post_id) 不带参数时,主要用来在循环输出文章列表中时,用来判断当前$post_id是否是置顶文章;带参数则是判断给定的$post_id是否是置顶文章。
</div><p><strong>【说明】</strong></p>
<p>检查当前文章是否置顶。返回值TRUE 或者 FALSE.</p>
<p><strong>【用法】</strong></p>
<p><?php is_sticky($post_ID); ?></p>
<p>【参数】</p>
<p>$post_ID</p>
<p>(string) (optional) 文章 ID<br />
默认: None<br />
返回值<br />
(boolean)<br />
<strong>True,或 false.</strong></p>
<p><strong>【示例】</strong></p>
<p><br />
is_sticky();</p>
<p>// 任意置顶文章被显示.</p>
<p>is_sticky('17');</p>
<p>// 当ID为17的文章被显示.</p>
<p><strong>【源文件】</strong></p>
<p>is_sticky() 位于 wp-<a href="/tags.php/include/" target="_blank">include</a>s/post.php.</p>
<p>/**<br />
* Check if post is sticky.<br />
*<br />
* Sticky posts should remain at the top of The Loop. If the post ID is not<br />
* given, then The Loop ID for the current post will be used.<br />
*<br />
* @since 2.7.0<br />
*<br />
* @param int $post_id Optional. Post ID.<br />
* @return bool Whether post is sticky.<br />
*/<br />
function is_sticky( $post_id = 0 ) {<br />
$post_id = absint( $post_id );</p>
<p> if ( ! $post_id )<br />
$post_id = get_the_ID();</p>
<p> $stickies = get_option( 'sticky_posts' );</p>
<p> if ( ! is_array( $stickies ) )<br />
return false;</p>
<p> if ( in_array( $post_id, $stickies ) )<br />
return true;</p>
<p> return false;<br />
}</p>
<p><strong>这里要举例说明的是:</strong></p>
<p> 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 就好了!</p>
<p><br />
这里还得感谢下友链中的tiandi兄在本站中留言中提醒说不存在“is_sticky($post_ID)中参数失效”的问题,指正自己对wordpress is_sticky($post_id)方法的错误理解。 |
|