Wordpress - 在类别存档中使用 wp_query - 如何显示适当的类别?
我在类别存档中使用 wp_query,以便可以使用 meta_query 忽略具有某些元值的帖子。
问题是,由于我使用的是wp_query,它似乎忽略了当前正在查看的类别并显示所有类别。
有没有办法检索用户正在查看的类别(可能由 url 定义)并将其插入到 wp_query 参数数组中?
我已经看到这个关于堆栈溢出的建议解决方案,但是必须有一种更简单的方法来做到这一点,因为当不使用默认循环时,wordpress 会自动显示正确的类别。
目前的代码:
$query = array(
'meta_query' => array(
array(
'key' => 'Display',
'value' => 'Yes',
)
),
'paged'=> $paged
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$pageposts = new WP_Query($query);
if ($pageposts):
while ( $pageposts->have_posts() ) : $pageposts->the_post();
I am using wp_query in a category archive so that I can use the meta_query to ignore posts with certain meta values.
The problem is that since I am using wp_query, it seems to ignore the category that is currently being viewed and displays all the categories.
Is there a way to retrieve the category (perhaps as defined by the url) that the user is viewing and insert it into the wp_query argument array?
I've seen this suggested solution on stack overflow, but there must be a simpler way to do it since when no the default loop is used, wordpress automatically displays the correct category.
The code currently:
$query = array(
'meta_query' => array(
array(
'key' => 'Display',
'value' => 'Yes',
)
),
'paged'=> $paged
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$pageposts = new WP_Query($query);
if ($pageposts):
while ( $pageposts->have_posts() ) : $pageposts->the_post();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,这是我自己能想到的最佳解决方案(使用 single_cat_title 设置变量):
Well, this is the best solution I could come up with on my own (using single_cat_title to set the variable):
我意识到这已经很旧了,但我也遇到了同样的问题。我使用的方法与您为我的类别存档提出的方法类似,但我还需要对 search.php 使用 WP Query,这导致我寻找解决方案。 WordPress Codex 有一种方法可以保留原始搜索查询,并且它似乎也适用于类别存档:
http://codex.wordpress.org/Creating_a_Search_Page#Preserving_Search_Page_Results_and_Pagination
应该只需添加您需要的参数即可开始。
I realize this is old, but I had the same problem. I used a method similar to what you came up with for my category archive, but I needed to use WP Query for search.php as well, which led me to looking for a solution. The wordpress codex has a way to preserve the original query for search, and it seems to work for a category archive as well:
http://codex.wordpress.org/Creating_a_Search_Page#Preserving_Search_Page_Results_and_Pagination
Should be able to just add the arguments you need and be good to go.
虽然 waffl 的解决方案有效,但我更喜欢使用“tax_query” 分类参数 因为tax_query是专门为按分类法查询而设计的。
While waffl's solution works, I prefer to use "tax_query" Taxonomy Parameters because tax_query is specifically designed to query by taxonomy.