自然排序的Wordpress帖子标题(按字母顺序和数字顺序)? [英] Natural sort Wordpress post titles (alphabetically and numerically)?

查看:162
本文介绍了自然排序的Wordpress帖子标题(按字母顺序和数字顺序)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过标题而不是字母顺序对新的Wordpress帖子查询进行标题排序?

Is there any possible way to sort a new Wordpress post query by the title, but numerically instead of alphabetically?

我有一些标题按字母顺序具有很多相同的名称,然后再有一个数字,因此当然,例如Wordpress将title12放在title1之前.

I have some titles that have a lot of the same name alphabetically, then have a number afterwards, so of course for example Wordpress is putting title12 ahead of title1.

$args = array( 
'orderby'=> 'title', 
'order' => 'ASC',
);
$loop = new WP_Query( $args );

我知道我们具有此功能,可以按升序对标题进行排序,但是它不能像这样对标题进行排序:

I know we have this functionality to sort titles in ascending order, but it does not sort titles like that:

Title 1
Title 2 

请让我知道我们是否可以使用WP查询吗? 感谢您的提前帮助:)

Please let me know if we any work around using WP query ? Thanks for your help in advance :)

推荐答案

尝试在上面的代码后立即添加:

Try adding this immediately after your code above:

usort($loop->posts, function($a,$b) {
   return strnatcmp($a->title, $b->post_title);
});

这篇关于自然排序的Wordpress帖子标题(按字母顺序和数字顺序)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆