欢迎来到入门教程网!

WordPress

当前位置:主页 > CMS教程 > WordPress >

wordpress获取置顶文章列表的方法

来源:本站原创|时间:2020-01-11|栏目:WordPress|点击:

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


复制代码
代码如下:

<?php
query_posts('posts_per_page=10&ignore_sticky_posts=1&orderby=rand');
while(have_posts()):the_post();
echo '<li>';the_title();echo '</li>';
endwhile;
wp_reset_query();

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

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


复制代码
代码如下:

$query_post = array(
'posts_per_page' => 10,
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 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站点实现分类目录订阅功能实例

栏    目:WordPress

下一篇:WordPress上传文件存放到不同目录的方法

本文标题:wordpress获取置顶文章列表的方法

本文地址:https://www.xiuzhanwang.com/a1/WordPress/12774.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有