首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将产品从Entrada主题中的特定页面中的产品类别中排除在外

将产品从Entrada主题中的特定页面中的产品类别中排除在外
EN

Stack Overflow用户
提问于 2018-07-30 13:38:48
回答 4查看 2.3K关注 0票数 3

我想排除所有的产品/旅游从伍尔商业类别‘远足’(段段式‘远足-ru’)在‘我们的旅游’页面(段式‘我们-旅游-ru’)。这是此页

我找到了这里的解决方案,所以我在下面使用了这段代码,但它对我不起作用。

找不到我的错误在哪里。

代码语言:javascript
复制
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
    $new_terms = array();
    // if a product category and on the shop page
    // to hide from shop page, replace is_page('YOUR_PAGE_SLUG') with is_shop()
    if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_page('our-tours-ru') ) {
        foreach ( $terms as $key => $term ) {
            if ( ! in_array( $term->slug, array( 'excursions-ru' ) ) ) {
                $new_terms[] = $term;
            }
        }
        $terms = $new_terms;
    }
    return $terms;
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-08-19 08:20:31

(由于现有的答案都不适用于您,而且您使用的是子主题,所以我发布了这个答案,它特定于您目前在https://themes.waituk.com/entrada-intro/上使用的https://blackseatravel.ge/ru/our-tours-ru/。)

正如我所看到的,“我们的旅游”页面使用的是“列出全宽度(三列网格)”模板,在父主题中,它位于entrada/entrada_templates/listing-full-width-3column-grid.php,它使用这个模板部分:entrada/template-parts/grid-threecolumn.php

如果打开它,就会看到这个自定义查询

代码语言:javascript
复制
$args = array(
        'post_type' => 'product',
        'posts_per_page' => $posts_per_page,
        'paged' => 1 
    );
$args = entrada_product_type_meta_query($args, 'tour' );
$loop = new WP_Query( $args );

用于“我们的旅游”页面中的初级产品部分/网格。

因此,要从该查询中排除“游走”类别,或者将产品排除在该类别之外,一种简单的方法(也可能有效)是将entrada/template-parts/grid-threecolumn.php复制到子主题文件夹(即entrada-child-picpic-v1_0/template-parts/grid-threecolumn.php),然后任意自定义$args数组。对于您的问题,请尝试在$loop = new WP_Query( $args );之前/之上添加以下内容

代码语言:javascript
复制
wp_reset_query(); // Just in case.
if ( is_page( 'our-tours-ru' ) ) {
    if ( ! isset( $args['tax_query'] ) ) {
        $args['tax_query'] = array();
    }

    // Excludes products from the "Excursions" category.
    $args['tax_query'][] = array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => array( 'excursions-ru' ),
        'operator' => 'NOT IN',
    );
}

注意:是其他答案中使用的相同的tax_query,除了上面的一个是专门针对listing-full-width-3column-grid.php模板(或使用grid-threecolumn.php模板部分的任何其他页面模板)。

票数 1
EN

Stack Overflow用户

发布于 2018-08-16 21:01:05

更新1 (见下文)

get_terms过滤器钩子不是用来过滤产品的,所以在您的情况下不方便。

Woocommerce有3个专用钩子,用于对产品查询进行自定义(过滤产品)

  • woocommerce_product_query (动作钩)
  • woocommerce_product_query_meta_query (过滤器钩)
  • woocommerce_product_query_tax_query (过滤器钩)

我们将使用最后一个,因为这是最好的一个为您的情况。

现在,要使它仅用于特定页面,我们将使用WordPress is_page()条件标记:

代码语言:javascript
复制
add_filter( 'woocommerce_product_query_tax_query', 'custom_exclude_products', 50, 2 );
function custom_exclude_products( $tax_query, $query ) {
    // Only on a specific page
    if( is_page('our-tours-ru') ){
        $tax_query[] = array(
            'taxonomy'  => 'product_cat',
            'field'     => 'slug',
            'terms'     => array('excursions-ru'),
            'operator'  => 'NOT IN'
        );
    }
    return $tax_query;
}

代码在您的活动子主题(或活动主题)的function.php文件中。测试和工作。

更新:

由于这是在页面上,您肯定使用Woocommerce的短代码在这个特定的页面上显示产品。

因此,在本例中,有一个专门用于Woocommerce短代码的钩子:

代码语言:javascript
复制
add_filter( 'woocommerce_shortcode_products_query', 'custom_shortcode_exclude_products', 50, 3 );
function custom_shortcode_exclude_products( $query_args, $atts, $loop_name ){
    if( is_page('our-tours-ru') ){
        $query_args['tax_query'] = array( array(
           'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => array( 'excursions-ru' ),
            'operator' => 'NOT IN'
        ) );
    }
    return $query_args;
}

代码在您的活动子主题(或活动主题)的function.php文件中。测试和工作…这对你也有好处。

你需要确保这个产品显示在一个的Wordpress页面上,这个页面中的片段是‘我们的旅游-ru’,因为如果没有答案,你会工作的。

最后一种使用pre_get_posts筛选器的可能性,主要适用于所有情况:

代码语言:javascript
复制
add_filter( 'pre_get_posts', 'custom_query_exclude_products', 900, 1 );
function custom_query_exclude_products( $query ) {
    if ( is_admin() || is_search() ) return $query;

    if ( is_page('our-tours-ru') ){
        $query->set( 'tax_query', array( array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'excursions ru' ),
            'operator' => 'NOT IN'
        ) ) );
    }

    remove_action( 'pre_get_posts', 'custom_query_exclude_products' );

    return $query;
}

代码在您的活动子主题(或活动主题)的function.php文件中。

票数 4
EN

Stack Overflow用户

发布于 2018-08-16 09:27:20

请试试下面的代码

代码语言:javascript
复制
function custom_pre_get_posts_query( $q ) {

$tax_query = (array) $q->get( 'tax_query' );

  $tax_query[] = array(
       'taxonomy' => 'product_cat',
       'field' => 'slug',
       'terms' => array( 'excursions-ru' ),
       'operator' => 'NOT IN'
  );

  $q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51595284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档