【說(shuō)明】
檢查當(dāng)前文章是否置頂。返回值TRUE 或者 FALSE.
【用法】
復(fù)制代碼
代碼如下:<?php is_sticky($post_ID); ?>
【參數(shù)】
$post_ID
(string) (optional) 文章 ID
默認(rèn): None
返回值
(boolean)
True,或 false.
【示例】
復(fù)制代碼
代碼如下:is_sticky();
// 任意置頂文章被顯示.</p> <p>is_sticky('17');
// 當(dāng)ID為17的文章被顯示.
【源文件】
is_sticky() 位于 wp-includes/post.php.
PHP Code復(fù)制內(nèi)容到剪貼板
- /**
- * Check if post is sticky.
- *
- * Sticky posts should remain at the top of The Loop. If the post ID is not
- * given, then The Loop ID for the current post will be used.
- *
- * @since 2.7.0
- *
- * @param int $post_id Optional. Post ID.
- * @return bool Whether post is sticky.
- */
- function is_sticky( $post_id = 0 ) {
- $post_id = absint( $post_id );
- if ( ! $post_id )
- $post_id = get_the_ID();
- $stickies = get_option( 'sticky_posts' );
- if ( ! is_array( $stickies ) )
- return false;
- if ( in_array( $post_id, $stickies ) )
- return true;
- return false;
- }
這里要舉例說(shuō)明的是:
is_sticky(10) 是判斷 $post_id為 10的文章是否是置頂文章,而不是說(shuō)所有置頂文章中post_id為 10的置頂文章。之所以會(huì)有后者錯(cuò)誤的理解,也是自己看了官方對(duì)于 is_sticky($post_id)方法用法文檔比較模糊的介紹,其實(shí)細(xì)究起來(lái),“所有置頂文章中post_id為 10的置頂文章” 這種判斷也是多余的,直接 $post->id==10 或 get_the_id()==10 判斷當(dāng)前文章$post_id是否等于10 就好了!
這里還得感謝下友鏈中的tiandi兄在本站中留言中提醒說(shuō)不存在“is_sticky($post_ID)中參數(shù)失效”的問(wèn)題,指正自己對(duì)wordpress is_sticky($post_id)方法的錯(cuò)誤理解。
【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過(guò)測(cè)試外,其他素材未做測(cè)試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請(qǐng)勿用于商業(yè)用途。如損害你的權(quán)益請(qǐng)聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。