wordpress怎么獲取置頂文章列表?
在WordPress中,或許你希望調(diào)用設(shè)置好的指定文章列表,這一功能如何實現(xiàn)呢?下文就介紹實現(xiàn)方法,大家參考使用吧
推薦:《WordPress教程》
首先,你需要了解query_posts函數(shù)。該函數(shù)的作用就是對文章進(jìn)行檢索、挑選、排序,在其后的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 '</li>'; endwhile; wp_reset_query();
將隨機列出一條文章的標(biāo)題。至于query_posts的具體參數(shù),請參考開發(fā)手冊。
接下來,我們就是要通過對query_posts的參數(shù)進(jìn)行調(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); ?> <ul style="display:none;"> <?php while(have_posts()):the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> <?php wp_reset_query();
參數(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ī)定的值,會用最新文章替補完整。)