wordpress/mysql 构建导航菜单的问题

发布于 2024-09-18 19:31:23 字数 671 浏览 4 评论 0原文

我正在开发一个 WordPress 主题,需要选择多个类别中的帖子来构建菜单。我可以将其设置为使用标签或标签和类别的组合来工作,但最终我需要能够通过多个猫/标签进行搜索。我能够找到的唯一允许在 cat/tag 上进行多重 AND 搜索的内置 WordPress 函数是 query_posts 函数,如下所示:

query_posts(array('category__and' => array(list_of_cats)))

有两个问题。首先是 wp 文档说不要使用它。第二个是一个真正的问题,因为我需要知道在运行循环之前查询了多少个帖子,因为我不想构建空的子菜单或其中只有一两个项目。

另一种方法是运行我自己的查询,但我不太清楚运行以查找所选类别中的帖子的正确语句。我可以想象搜索两个类别需要发生什么,但在 3+ 时它会变得模糊。

我可以执行多个独立的查询,然后使用 array_intersect 来查找与所有查询匹配的查询,但我想在这里扩展一下我的 mysql 和/或 wp 知识。

总之,我希望回答以下任何一个问题:

1 - query_posts() 的替代方案,允许多个 AND 匹配
2 - 一种查找使用 query_posts() 时将循环的帖子数量的方法
3 - 一些 mysql 魔法可以对同一个表的多个查询运行一个大联合

提前致谢

I am working on a wordpress theme and need to select posts that are in multiple categories to build a menu. I could set it up to work off of the tags, or a combination of tags and categories, but in the end I need to be able to search by multiple cat/tag. The only built-in wordpress function that I have been able to find that allows for a multiple AND search on cat/tag is the query_posts function as such:

query_posts(array('category__and' => array(list_of_cats)))

There are two problems with that. The first is that the wp docs say not to use it. The second is a real problem in that I need to know how many posts were queried before running my loop since I don't want to build submenus that are empty or only have one or two items in them.

The alternative is to run my own query but I can't quite figure out the right statement to run to find posts in the selected categories. I can visualize what needs to happen to search for two categories, but it gets fuzzy at 3+.

I can execute multiple, independent queries and then use array_intersect to find the those that match all queries but I would like to expand my mysql and/or wp knowledge a bit here.

In summary, I would appreciate an answer to any one of the following questions:

1 - an alternative to query_posts() that allows for multiple AND matching
2 - a way to find the number of posts that will be looped when using query_posts()
3 - some mysql magic that runs a big union on multiple queries to the same table

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南月 2024-09-25 19:31:23

“如果您想在主循环之外创建单独的循环,则应该使用 get_posts() 注意:从

版本 3.0 开始,页面 ID 数组也可用于包含和排除参数。”

很确定您可以执行以下操作:

$wp_query->post_count

获取查询帖子的总数。

是的,根据 WPQuery:

获取帖子()
获取并返回请求的帖子
从数据库中。还填充
$posts 和 $post_count。

:)

"If you want to create separate Loops outside of the main one, you should use get_posts() instead.

Note: Beginning with Version 3.0, an array of Page ID also can be used for the include and exclude parameters."

Pretty sure you can do something like:

$wp_query->post_count

To get the total number of queried posts.

Yeah, according to WPQuery:

get_posts()
Fetch and return the requested posts
from the database. Also populate
$posts and $post_count.

:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文