非插件调用 WordPress 置顶文章列表

WordPress教程 10853

使用 WordPress 写博客的时候,偶尔会把几篇自己认为优秀的文章设置为置顶显示以推荐给读者,通常 WP 的置顶文章是显示在首页顶部的,但有时候制作一些特殊的主题可能会因为各种需要要把 WP 博客的置顶文章另外调用出来显示,那么博主可以通过使用下面的这段代码来实现该效果。

调用 WordPress 置顶文章列表:

在需要调用 WordPress 置顶文章的地方直接添加以下代码即可:

1
2
3
4
5
6
7
8
9
10
11
12
<ul>
<?php
	$sticky = get_option('sticky_posts');
	rsort( $sticky );
	$sticky = array_slice( $sticky, 0, 5);
	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;  wp_reset_query(); ?>
</ul>

提示:如果博客没有置顶文章,会自动显示博客最新文章列表。

精品推荐: