2009-06-19 13:09:37
793点击

获取WordPress的最新置顶文章

如下代码所示,获取WordPress的最新2条置顶文章:

<?php
/* 获取所有置顶文章*/
$sticky = get_option( ‘sticky_posts’ );

/* 排序,让最新的文章在前 */
rsort( $sticky );

/* 获取2条置顶文章(可以更改这个数目) */
$sticky = array_slice( $sticky, 0, 2 );

/* 查询置顶文章 */
query_posts( array( ‘post__in’ => $sticky, ‘caller_get_posts’ => 1 ) );

<?php while (have_posts()) : the_post(); ?>

<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>

<?php endwhile; ?>
?>

必须 使用rsort()函数把置顶文章按文章ID排序(最新的在前),因为文章置顶时不是按ID加入数组的…

Leave a Reply

最新文章

最新评论

点击最多

分类存档

合作伙伴