搜索
Hi~登录注册
查看: 1607|回复: 0

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

[复制链接]

1892

主题

1899

帖子

6406

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6406
发表于 2015-5-14 13:49:28 | 显示全部楼层 |阅读模式
wordpress is_sticky($post_id) 不带参数时,主要用来在循环输出文章列表中时,用来判断当前$post_id是否是置顶文章;带参数则是判断给定的$post_id是否是置顶文章。
</div><p><strong>【说明】</strong></p>
<p>检查当前文章是否置顶。返回值TRUE 或者 FALSE.</p>
<p><strong>【用法】</strong></p>
<p>&lt;?php is_sticky($post_ID); ?&gt;</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 />
&nbsp;* Check if post is sticky.<br />
&nbsp;*<br />
&nbsp;* Sticky posts should remain at the top of The Loop. If the post ID is not<br />
&nbsp;* given, then The Loop ID for the current post will be used.<br />
&nbsp;*<br />
&nbsp;* @since 2.7.0<br />
&nbsp;*<br />
&nbsp;* @param int $post_id Optional. Post ID.<br />
&nbsp;* @return bool Whether post is sticky.<br />
&nbsp;*/<br />
function is_sticky( $post_id = 0 ) {<br />
&nbsp;$post_id = absint( $post_id );</p>
<p>&nbsp;if ( ! $post_id )<br />
&nbsp; $post_id = get_the_ID();</p>
<p>&nbsp;$stickies = get_option( 'sticky_posts' );</p>
<p>&nbsp;if ( ! is_array( $stickies ) )<br />
&nbsp; return false;</p>
<p>&nbsp;if ( in_array( $post_id, $stickies ) )<br />
&nbsp; return true;</p>
<p>&nbsp;return false;<br />
}</p>
<p><strong>这里要举例说明的是:</strong></p>
<p>&nbsp;is_sticky(10) 是判断 $post_id为 10的文章是否是置顶文章,而不是说所有置顶文章中post_id为 10的置顶文章。之所以会有后者错误的理解,也是自己看了官方对于 is_sticky($post_id)方法用法文档比较模糊的介绍,其实细究起来,&ldquo;所有置顶文章中post_id为 10的置顶文章&rdquo; 这种判断也是多余的,直接 $post-&gt;id==10&nbsp; 或 get_the_id()==10 判断当前文章$post_id是否等于10 就好了!</p>
<p><br />
这里还得感谢下友链中的tiandi兄在本站中留言中提醒说不存在&ldquo;is_sticky($post_ID)中参数失效&rdquo;的问题,指正自己对wordpress is_sticky($post_id)方法的错误理解。
公众微信:idc5ahl
公众QQ:吾爱互联
关注公众微信,公众QQ每天领现金卡密
卡密介绍(http://www.5ahl.com/thread-2182-1-1.html
回复

使用道具 举报

游客
回复
您需要登录后才可以回帖 登录 | 点我注册

快速回复 返回顶部 返回列表