wordpress中有一个很好用的功能--置顶, 现在来研究下使用方法:

设置:

在列表页点快速编辑(Quick Edit), 然后把置顶勾上,如图


使用:

  1. ......
  2. $sticky = get_option('sticky_posts');
  3. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  4. query_posts(array('post_type'  => 'post','post__in' => $sticky , 'cat' => 332,  'paged' => $paged, 'posts_per_page' => 15));
  5.     if(have_posts()):
  6. ......

这个显示出来的都是置顶的文章,那么如何在文章列表页既显示置顶的文章又显示非置顶的文章呢,并且置顶的文章要放在前面,方法是:
代码如下
  1. ......
  2. //先获取置顶的文章
  3. $sticky = get_option('sticky_posts');
  4. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;         query_posts(array('post_type'  => 'post','post__in' => $sticky , 'cat' => 223,  'paged' => $paged, 'posts_per_page' => 15));
  5.  
  6. if(have_posts() && count($sticky) > 0):  //不要忘了这里的条件,否则会出现重复内容
  7.   while (have_posts()) :
  8.     the_post();
  9.   ......
  10.   endwhile;
  11. endif;
  12.  
  13. //再正常获取文章
  14. query_posts(array('post_type'  => 'post', 'cat' => 223,  'paged' => $paged, 'posts_per_page' => 15));
  15. if(have_posts()):
  16.   while (have_posts()) :
  17.     the_post();
  18.     //如果普通文章里面含有置顶文章,就跳过
  19.     if(in_array(get_the_ID(),$sticky))  continue;
  20.   ......
  21.   endwhile;
  22. endif;
  23.  




随便说说

还可以输200