WordPress可以通过各种自定义配置,实现各种各样的需求,WordPress默认文章列表排序方式是根据发布时间,也就是最新的文章显示在列表最前面,那么如何按修改时间、按评论数及阅读量排序?今天为大家分享query_posts($args)数据查询来说,通过配置其中的orderby参数来实现各种排序。

<?php

$args = array(

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post_type\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post_status\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’publish\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’posts_per_page\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’10\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’orderby\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’date\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’order\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’DESC\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’caller_get_posts\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => 1,

);

$query_posts = new WP_Query();

$query_posts->query($args);

while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>

<li>

<a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”https://www.wpzt.net/<?php the_permalink(); ?>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” title=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”<?php the_title(); ?>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”>

<?php the_title(); ?>

</a>

</li>

<?php } wp_reset_query();?>

上述查询函数中,我们通过修改orderby后面的值来,实现不同的排序方式。

一般用法:

按发布日期排序 orderby=date?

按修改时间排序 orderby=modified

按文章ID排序 orderby=ID

按评论最多排序 orderby=comment_count

按标题排序 orderby=title

随机排序 orderby=rand

特殊用法:

如果我们想通过浏览量来排序呢?要知道WordPress默认是没有浏览量这个功能的,但是大多数的用户都通过主题或者插件实现了文章阅读量,其原理无一例外是通过自定义栏目增加阅读量的统计。因此我们可以通过自定义栏目的值大小来实现阅读量排序。首先确定你的文章阅读量的自定义蓝色名称,一般为views 然后我们将查询函数进行修改即可,得到如下代码:

<?php

$args = array(

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post_type\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post_status\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’publish\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’posts_per_page\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’10\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, /* 显示几条 */

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’meta_key\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’views\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,/* 此处为你的自定义栏目名称 */

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’orderby\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’meta_value_num\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, /* 配置排序方式为自定义栏目值 */

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’order\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’DESC\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, /* 降序排列 */

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’caller_get_posts\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => 1,

);

$query_posts = new WP_Query();

$query_posts->query($args);

while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>

<li>

<a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”https://www.wpzt.net/<?php the_permalink(); ?>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” title=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”<?php the_title(); ?>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”>

<?php the_title(); ?>

</a>

</li>

<?php } wp_reset_query();?>

还有一个常见的查询函数:

<?php

$posts = get_posts(“numberposts=10&meta_key=views&orderby=meta_value_num&order=desc”);

foreach( $posts as $post ) :

?>

……

<?php endforeach; ?>

WordPress是非常强大的,通过各种自定义配置,可以实现各种各样的需求。

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部