wordpress怎么獲取置頂文章列表?
在WordPress中,或許你希望調(diào)用設(shè)置好的指定文章列表,這一功能如何實現(xiàn)呢?下文就介紹實現(xiàn)方法,大家參考使用吧
?推薦:《WordPress教程》
首先,你需要了解query_posts函數(shù)。該函數(shù)的作用就是對文章進行檢索、挑選、排序,在其后的LOOP循環(huán)中使用經(jīng)過挑選、排序的文章。例如:
代碼如下
<?php query_posts('posts_per_page=10&ignore_sticky_posts=1&orderby=rand'); while(have_posts()):the_post(); echo '<li>';the_title();echo?''; endwhile; wp_reset_query();
將隨機列出一條文章的標題。至于query_posts的具體參數(shù),請參考開發(fā)手冊。
接下來,我們就是要通過對query_posts的參數(shù)進行調(diào)整,挑選出置頂?shù)奈恼铝斜砹恕?/p>
代碼如下:
$query_post?=?array( 'posts_per_page'?=>?10, 'post__in'?=>?get_option('sticky_posts'), 'caller_get_posts'?=>?1 ); query_posts($query_post); ?>
參數(shù)用一個數(shù)組的形式放在$query_post中,關(guān)鍵的參數(shù)為’post__in’ =>get_option(‘sticky_posts’)和’caller_get_posts’ => 0。
‘post__in’ => get_option(‘sticky_posts’)確定了該LOOP調(diào)用的是置頂文章列表。’caller_get_posts’的作用是排除非指定性文章,即除了置頂文章之外,不顯示其他的文章。(不添加的情況下,如果置頂文章條目不足’posts_per_page’規(guī)定的值,會用最新文章替補完整。)
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END
喜歡就支持一下吧
相關(guān)推薦