Wordpress - 在类别存档中使用 wp_query - 如何显示适当的类别?

发布于 2024-12-05 15:44:28 字数 750 浏览 0 评论 0原文

我在类别存档中使用 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 技术交流群。

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

发布评论

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

评论(3

上课铃就是安魂曲 2024-12-12 15:44:28

好吧,这是我自己能想到的最佳解决方案(使用 single_cat_title 设置变量):

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

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);
故事↓在人 2024-12-12 15:44:28

我意识到这已经很旧了,但我也遇到了同样的问题。我使用的方法与您为我的类别存档提出的方法类似,但我还需要对 search.php 使用 WP Query,这导致我寻找解决方案。 WordPress Codex 有一种方法可以保留原始搜索查询,并且它似乎也适用于类别存档:

<?php
global $query_string;

$query_args = explode("&", $query_string);
$search_query = array();

foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search = new WP_Query($search_query);
?>

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:

<?php
global $query_string;

$query_args = explode("&", $query_string);
$search_query = array();

foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search = new WP_Query($search_query);
?>

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.

奈何桥上唱咆哮 2024-12-12 15:44:28

虽然 waffl 的解决方案有效,但我更喜欢使用“tax_query” 分类参数 因为tax_query是专门为按分类法查询而设计的。

$term = get_queried_object();
$termID = $term->term_taxonomy_id;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = array(
    'tax_query' => array( 
            array(
                'taxonomy' => 'category', //you change this to a custom taxonomy if needed
                'field' => 'term_id',
                'terms' => $termID
            )
        ),

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

While waffl's solution works, I prefer to use "tax_query" Taxonomy Parameters because tax_query is specifically designed to query by taxonomy.

$term = get_queried_object();
$termID = $term->term_taxonomy_id;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = array(
    'tax_query' => array( 
            array(
                'taxonomy' => 'category', //you change this to a custom taxonomy if needed
                'field' => 'term_id',
                'terms' => $termID
            )
        ),

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