wordpress怎么调用置顶文章,调用置顶文章wordpress教程

大话运营教研室 736 0

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

wordpress怎么调用置顶文章,调用置顶文章wordpress教程-第1张图片

1、实现功能所需要的代码如下:

<?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; ?>

2、第一行给$sticky赋值;

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

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

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

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

抱歉,评论功能暂时关闭!