wordpress置顶文章调用与设置

wordpress网站后台允许我们设置一些置顶文章,然后通过调用置顶文章的代码把它调用到自己的网站前台。下面是最常用的wordpress调用置顶文章代码,适用于所有模板使用。

<?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(); ?>” target=”_blank”><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>

代码解释:
rsort( $sticky ); 对置顶文章数组逆向排序,即大ID在前;
$sticky = array_slice( $sticky, 0, 5);控制显示置顶文章的数量,输出置顶文章数,请修改5,0不要动,如果需要全部置顶文章输出,可以把这句注释掉;
‘post__in’ => get_option(‘sticky_posts’)确定了该LOOP调用的是置顶文章列表。
‘caller_get_posts’的作用是排除非指定性文章,即除了置顶文章之外,不显示其他的文章。

wordpress文章置顶方法:

【后台】–【文章】–【编辑】—【将文章置顶到顶部】