按标题对 WordPress 帖子进行排序,忽略诸如“the”、“a”、“an”之类的文章
我按标题的字母顺序对帖子进行排序,如下所示:
<?php
{
$posts = get_posts($query_string .
'&orderby=title&order=asc&posts_per_page=-1');
}
get_template_part( 'loop', 'category' );
?>
我想从排序中排除“the”、“a”和“an”等文章。
实现这一目标的最佳方法是什么?
谢谢!
I'm sorting my posts alphabetically by Title, like so:
<?php
{
$posts = get_posts($query_string .
'&orderby=title&order=asc&posts_per_page=-1');
}
get_template_part( 'loop', 'category' );
?>
I'd like to exclude articles such as "the", "a", and "an" from the sort.
What would be the best way to accomplish this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了实现这一点,你需要添加 自定义元字段到帖子。将其命名为 mytitle(例如)。
对于您添加的新帖子,很简单,您必须在添加帖子页面的 mytitle 自定义字段中添加修改后的标题(从标题中删除 a、an、the)。
对于旧帖子,这有点棘手,您必须编写一段 php 代码来检索帖子的标题,使用 php preg_replace 从中删除 'a'、'an'、'the' 并添加它使用类似以下内容到 WordPress 数据库的 postmeta 表:
其中 $postid 是循环内的帖子 ID,$title 是修改后的标题。
现在要显示,您必须使用自定义循环(不是主题中包含的循环)。
以下是如何创建一个基本的自定义循环来显示按 mytitle 顺序排序的帖子。
现在您可以通过任何您想要的方式执行查询。 Wordpres 提供了多种方法来执行此操作。 这是一个链接
例如,您可以执行以下操作
For achieving this you need to add a custom meta field to the post. Name it mytitle (say).
For the new posts you add, it is simple, you have to add your modified title(removing a, an, the from the title) in the mytitle custom field in the add posts page.
For old posts it is a bit tricky, you have to write a php code to retrieve the titles of the post remove 'a','an','the' from them using php preg_replace and add it to the postmeta table of your wordpress database using something like this:
where $postid is the post id inside the loop and $title is your modified title.
Now to display, you have to use a custom loop (not the loop included in the theme).
Here is how you can make a basic custom loop to display posts sorted in order of mytitle.
Now you can execute the query by any means you want. Wordpres provides various methods to do so. Here's a link
For example you can do something like this
实际上,您可以通过使用
posts_orderby_request
过滤器操作 ORDERBY 子句并使用TRIM
忽略冠词“the”、“a”和“an”来实现此目的。这是一个例子:You can actually achieve this by manipulating the ORDERBY clause using the
posts_orderby_request
filter, and usingTRIM
to ignore articles "the", "a", and "an". Here's an example: