您的位置: 首页 > 参考资料 > 网站建设 > WordPress

WordPress文章置顶功能的一些应用代码

时间: 2011-08-1 20:18 / 围观: 5,097 / 评论数: 0 条 /
分享到:

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

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

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

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

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

返回第一篇置顶文章

$sticky=get_option('sticky_posts') ;
query_posts('p=' . $sticky[0]);


$args = array(
‘posts_per_page’ => 1,
‘post__in’ => get_option(‘sticky_posts’),
‘caller_get_posts’ => 1
);
query_posts($args);

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

$sticky = get_option('sticky_posts');
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'caller_get_posts' => 1
);
query_posts($args);
if($sticky[0]) {
// insert here your stuff...
}

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

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

返回某一分类下所有文章,完全不显示置顶文章,保留分页
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
'cat'=>3,
'caller_get_posts'=>1,
'post__not_in' => $sticky,
'paged'=>$paged,
);
query_posts($args);

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

本文链接:http://www.ppcn.net/wordpress-sticky.html
(本站如未注明转载外,均为原创文章版权所有,转载请务必注明本文出处及链接)
上一篇:
下一篇:

暂时还没有评论.

登陆 后发表评论.