Elementor 循环网格的后分类自定义查询不起作用

问题描述 投票:0回答:1

我试图在名为“verticals”的自定义分类术语的存档页面上仅显示子术语。

function filter_elementor_loop_widget_terms($query) {
    // Check if it's a taxonomy archive for 'verticals'
    if (is_tax('verticals')) {
        // Get the current term object
        $current_term = get_queried_object();

        // Get child terms of the current term
        $args = array(
            'taxonomy'   => 'verticals',
            'child_of'   => $current_term->term_id,
            'hide_empty' => false,  // Set to true if you want to hide empty terms
            'fields'     => 'ids', // We only need the IDs
        );

        $child_terms = get_terms($args);

        if (!empty($child_terms) && !is_wp_error($child_terms)) {
            // Modify the query to include only child terms
            $query->set('post_type', 'any'); // This ensures we're adding a tax_query and not replacing it
            $query->set('tax_query', array(
                array(
                    'taxonomy' => 'verticals',
                    'field'    => 'term_id',
                    'terms'    => $child_terms,
                ),
            ));
        } else {
            // Ensure no results are returned if no child terms are found
            $query->set('tax_query', array(
                array(
                    'taxonomy' => 'verticals',
                    'field'    => 'term_id',
                    'terms'    => array(0), // This ensures no results
                ),
            ));
        }
    }
}
add_action('elementor/query/child_verticals_terms', 'filter_elementor_loop_widget_terms');

这根本不显示任何内容。我也尝试过其他选择,但没有运气。

我什至尝试为要显示的术语提供查询手册 ID,但这也不起作用。

所以我假设我尝试应用这个钩子的方式有问题。

在本书的正下方,我创建了另一个钩子来按标题过滤帖子,效果很好。

php hook elementor taxonomy
1个回答
0
投票

我遇到了同样的问题,这似乎是当前 Elementor 版本中的一个错误。这是讨论同一问题的 GitHub 帖子:https://github.com/elementor/elementor/issues/27647

本质上,一旦您将任何查询 ID 输入到分类循环网格中,它就会停止工作。即使该查询 ID 甚至没有在代码中过滤。

幸运的是,有一个使用过滤器的解决方法

elementor/loop_taxonomy/args

add_filter('elementor/loop_taxonomy/args', function(array $args): array
{
    return array_merge($args, [
        // any term query args
    ]);
});
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.