首页 > CMS教程 > &#&按 > wordpress怎么获取置顶文章列表

wordpress怎么获取置顶文章列表

藏色散人
发布: 2019-12-07 11:00:09
原创
3453 人浏览过

wordpress怎么获取置顶文章列表

wordpress怎么获取置顶文章列表?

在WordPress中,或许你希望调用设置好的指定文章列表,这一功能如何实现呢?下文就介绍实现方法,大家参考使用吧

 推荐:《WordPress教程

首先,你需要了解query_posts函数。该函数的作用就是对文章进行检索、挑选、排序,在其后的LOOP循环中使用经过挑选、排序的文章。例如:

代码如下

1

2

3

4

5

6

<?php

query_posts(&#39;posts_per_page=10&ignore_sticky_posts=1&orderby=rand&#39;);

while(have_posts()):the_post();

echo &#39;<li>&#39;;the_title();echo &#39;</li>&#39;;

endwhile;

wp_reset_query();

登录后复制

将随机列出一条文章的标题。至于query_posts的具体参数,请参考开发手册。

接下来,我们就是要通过对query_posts的参数进行调整,挑选出置顶的文章列表了。

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

$query_post = array(

&#39;posts_per_page&#39; => 10,

&#39;post__in&#39; => get_option(&#39;sticky_posts&#39;),

&#39;caller_get_posts&#39; => 1

);

query_posts($query_post);

?>

<ul style="display:none;">

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

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

<?php endwhile; ?>

</ul>

<?php

wp_reset_query();

登录后复制

参数用一个数组的形式放在$query_post中,关键的参数为'post__in' =>get_option('sticky_posts')和'caller_get_posts' => 0。

'post__in' => get_option('sticky_posts')确定了该LOOP调用的是置顶文章列表。'caller_get_posts'的作用是排除非指定性文章,即除了置顶文章之外,不显示其他的文章。(不添加的情况下,如果置顶文章条目不足'posts_per_page'规定的值,会用最新文章替补完整。)

以上是wordpress怎么获取置顶文章列表的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板