有的時候我們會在特殊位置顯示一些網站的置頂文章,那麼我們需要如何操作呢?
首先設定幾篇文章為置頂文章,這裡要說明的是,設定置頂文章不需要在文章編輯頁面,而是文章的快速編輯裡有置頂按鈕
然後,在需要顯示置頂文章的地方,插入下面的程式碼,即可呼叫 WordPress 獲取指定文章的方法

- <ul>
- <?php $sticky = get_option('sticky_posts'); rsort( $sticky );
- $sticky = array_slice( $sticky, 0, 2);query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
- if (have_posts()) :while (have_posts()) : the_post();
- ?>
- <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
- <?php endwhile; endif; ?>
- </ul>