Chinaunix首页 | 论坛 | 博客
  • 博客访问: 378127
  • 博文数量: 165
  • 博客积分: 436
  • 博客等级: 下士
  • 技术积分: 887
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-10 02:49
文章分类

全部博文(165)

文章存档

2012年(95)

2011年(70)

分类:

2011-12-01 00:19:45

原文地址:wordpress文章置顶相关内容 作者:netwenk

通过WordPress本身带有的置顶功能函数,可以实现很多与置顶功能有关的其他应用,比如下面的一些代码。下面函数在最新版WordPress3.2.1版测试通过。

提取某分类的最新置顶文章

代码如下:

  1. <?php query_posts(array('cat'=>73,'posts_per_page' => 5,'post__in' => get_option('sticky_posts'),'caller_get_posts' => 1));?>
  2. <?php if(have_posts()):while(have_posts()):the_post(); ?>
  3. <li><span>·</span><a href="" target="_blank"><?php echo cut_str($post->post_title,36); ?></a></li>
  4. <?php endwhile;?>
  5. <?php else:?>
  6. <?php endif;wp_reset_query();?>


在查询中,被设为“置顶”的文章会显示在其它文章之前,除非该文章已经被caller_get_posts=1参数排除。

  • array('post__in'=>get_option('sticky_posts')) —— 返回所有置顶文章的数组
  • caller_get_posts=1 —— 排除返回的文章上方的置顶文章,但在返回文章列表时,以自然顺序将曾经置顶的文章安插在列表中。

返回第一篇置顶文章


  1. $sticky=get_option('sticky_posts') ;
  2. query_posts('p=' . $sticky[0]);
  3. $args = array(
  4. ‘posts_per_page’ => 1,
  5. ‘post__in’ => get_option(‘sticky_posts’),
  6. ‘caller_get_posts’ => 1
  7. );
  8. query_posts($args);
  9. ?>

返回第一篇置顶文章;若无,则不返回任何内容

  1. $sticky = get_option('sticky_posts');
  2. $args = array(
  3. 'posts_per_page' => 1,
  4. 'post__in' => $sticky,
  5. 'caller_get_posts' => 1
  6. );
  7. query_posts($args);
  8. if($sticky[0]) {
  9. // insert here your stuff...
  10. }

从查询中排除所有置顶文章
query_posts(array("post__not_in" =>get_option("sticky_posts")));

返回某一分类下所有文章,但不在文章列表上方显示置顶文章。所有设为“置顶”的文章以正常顺序(如日期顺序)显示
query_posts('caller_get_posts=1&posts_per_page=3&cat=6');

返回某一分类下所有文章,完全不显示置顶文章,保留分页

  1. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  2. $sticky=get_option('sticky_posts');
  3. $args=array(
  4. 'cat'=>3,
  5. 'caller_get_posts'=>1,
  6. 'post__not_in' => $sticky,
  7. 'paged'=>$paged,
  8. );
  9. query_posts($args);

上面是一些基本的WordPress文章置顶功能的应用代码,抛砖引玉,大家可根据需求灵活借鉴。

阅读(1088) | 评论(0) | 转发(0) |
0

上一篇:Linux下更改时间

下一篇:Linux审计配置

给主人留下些什么吧!~~