将帖子和页面与 WordPress 搜索结果分开
我正在寻找一种在我的自定义 WordPress 主题中呈现搜索结果的方法。
我希望能够呈现这样的结果:
Displaying 4 search results for "test"
Pages
- testpage 1
- testpage 2
Posts
- testpost 1
- testpost 2
我写了一个函数 is_type_page
我可以在循环(2 个循环)内使用它,但这会破坏分页功能。
有什么建议如何实现这一目标?
I am looking for a way to present search results in my custom wordpress theme.
I was hoping being able to present the results like this:
Displaying 4 search results for "test"
Pages
- testpage 1
- testpage 2
Posts
- testpost 1
- testpost 2
I wrote a function is_type_page
that I can use inside the loop (2 loops), but this breaks the pagination functionality.
Any suggestions how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将在页面上运行 2 个单独的循环,在页面的第一个循环运行 rewind_posts() 之后,然后再次运行循环。此外,分页的关键是确保两个循环都选择全局 $paged 变量。 $paged 是 WordPress 将帖子分成页面的方式。即,如果您转到某些内容的第 2 页,则全局 $paged = 2。
希望这有助于
多个循环 在此处使用 rewind_posts
I would run 2 separate loops on the page, after the first loop for pages run rewind_posts() and then run the loop again. Also the key to pagination is making sure the global $paged variable is being picked up on by both loops. $paged is how wordpress separates posts into pages. i.e. if you go to page 2 of something then the global $paged = 2.
Hope that helps
Multiple loops using rewind_posts here
如果您希望它们以单独的标题显示,则可以运行两个循环。这是让它们在创建日期出现时显示混合的代码......
Running two loops is the way to go if you want them displayed with separate headers. Here is code to get them to show intermingles as they come up by date created...