记录生活
简单不先于复杂

wordpress怎么调用置顶文章?wordpress调用置顶文章的方法

本文来给大家分享下wordpress调用置顶文章的方法,不多说,直接上代码:

<?php  
$sticky = get_option('sticky_posts');  
rsort( $sticky );  
$sticky = array_slice( $sticky, 0, 1);  
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );  
if (have_posts()) :  
while (have_posts()) : the_post();  
?>  
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>

fa823626804a092

以上代码的解释:

第一行给$sticky赋值;

第二行rsort( $sticky ); 对置顶文章数组逆向排序,即大ID在前;

第三行$sticky = array_slice( $sticky, 0, 1);控制显示置顶文章的数量,仅修改数字1即可,其他参数不要动,如果输出全部的置顶文章,删掉这一行即可;

第四行里面的'post__in' => get_option('sticky_posts')确定了该LOOP调用的是置顶文章列表;'caller_get_posts'这个参数的作用是排除不是置顶的文章。

接下来就是循环了,循环里面的代码就是正常调用的代码即可。

赞(0)
未经允许不得转载:爱安普 » wordpress怎么调用置顶文章?wordpress调用置顶文章的方法