Wordpress - 在类别存档中使用 wp_query - 如何显示适当的类别? [英] Wordpress - Use wp_query in category archive - how to display the appropriate category?

查看:20
本文介绍了Wordpress - 在类别存档中使用 wp_query - 如何显示适当的类别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类别档案中使用 wp_query,以便我可以使用 meta_query 忽略具有某些元值的帖子.

I am using wp_query in a category archive so that I can use the meta_query to ignore posts with certain meta values.

问题在于,由于我使用的是 wp_query,它似乎忽略了当前正在查看的类别并显示所有类别.

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.

有没有办法检索用户正在查看的类别(可能由 url 定义)并将其插入 wp_query 参数数组?

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?

我已经看到了这个关于堆栈溢出的建议解决方案,但必须有一个更简单的方法来做到这一点,因为当没有使用默认循环时,wordpress 会自动显示正确的类别.

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.

当前代码:

$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();

推荐答案

嗯,这是我自己能想到的最好的解决方案(使用 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):

$currentCategory = single_cat_title("", false);

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = array(
    'category_name' => $currentCategory,

    'paged'=> $paged,
    'posts_per_page' => '15'
);
$pageposts = new WP_Query($query);

这篇关于Wordpress - 在类别存档中使用 wp_query - 如何显示适当的类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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