【问题标题】:Exclude products from a product category on a specific page in Entrada theme在 Entrada 主题的特定页面上从产品类别中排除产品
【发布时间】:2019-01-06 18:45:56
【问题描述】:

我想从“我们的旅游”页面(slug 'our-tours-ru')上的 woocommerce 类别“Excursions”(slug 'excursions-ru')中排除所有产品/旅游。这里是this page

我找到了this solution here 所以我在下面使用了这段代码,但它对我不起作用。

找不到我的错误在哪里。

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;
}

【问题讨论】:

  • 这么难的问题?
  • 您好,您正在使用的功能是用于其他目的,此功能是从商店页面而不是产品中隐藏/删除类别。

标签: wordpress woocommerce categories product hook-woocommerce


【解决方案1】:

更新 1 (见下文)

get_terms 过滤钩不是为过滤产品而设计的,所以对你来说不方便。

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

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

我们将使用最后一个,因为它最适合您的情况。

现在要让它仅对特定页面有效,我们将使用 WordPress is_page() 条件标签:

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 短代码有一个专用挂钩:

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 页面上,其中 slug 是“our-tours-ru”,因为如果不是,您将无法获得任何答案。


最后一种可能适用于大多数情况,使用pre_get_posts 过滤器:

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 文件中。

【讨论】:

  • 谢谢,但它对我不起作用。添加在孩子的function.php中。测试了几次。
  • @Merkucio 我已经为我的答案添加了更新......这真的很复杂,因为我们不知道它是否真的在页面上,而且我们不知道你是如何展示产品的那个页面。我已经测试了我的代码,创建了一个 slug 为 our-tours-ru 的页面,创建了一个 slug 为 excursions-ru 的产品类别,并将其分配给我想使用我的代码排除的一些产品……然后使用简码 [products]它可以工作并从excursions-ru 产品类别中排除产品。尝试使用更多详细信息更新您的答案,以获得对您问题的有效答案。
  • 我认为这取决于 Entrada 主题。在这个主题中进行了太多的定制,所以常规的钩子和方法不能代替其他主题工作。但是 Sally CJ 的方法是有效的。无论如何,感谢您的帮助。
  • 我不使用 Entrada 主题。你甚至没有在你的问题中提到它......我已经在标题上添加了它。我的代码主要适用于其他主题。
  • 可以根据页面名称的 div id 调整执行吗?
【解决方案2】:

(由于现有答案都不适合您并且您使用的是子主题,因此我发布了此答案,该答案特定于您当前在 your site 上使用的 Entrada theme。) em>

如我所见,“我们的旅行”页面使用的是“Listing Full Width(三列网格)”模板,该模板在父主题中位于entrada/entrada_templates/listing-full-width-3column-grid.php ,并且使用了这个模板部分entrada/template-parts/grid-threecolumn.php.

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

$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 ); 之前/上方添加此内容:

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 模板部分)。

【讨论】:

  • 嗨@Merkucio,感谢您接受我的回答。 =) 你可以像这样添加另一个页面:is_page( array( 'out-tours-ru', 'slug', 'slug' ) ) 其中“slug”是页面 slug,同样对于类别:array( 'excursions-ru, 'slug', 'slug')` 其中“slug”是术语/类别蛞蝓。
  • 我的意思是,感谢您的赏金。并为代码格式道歉 - 它应该是array( 'excursions-ru, 'slug', 'slug' )。如果您需要进一步的帮助,请告诉我。 :)
  • 它应该是is_page( array( 'our-tours-ru', 'our-tours' ) ) 而不是is_page( 'out-tours-ru', 'our-tours' )。再试一次?确保使用array() 并检查拼写.. 比如our 而不是out.. :)
  • 对于类别,'terms' => array( 'excursions-ru', 'excursions' ),。再次对错别字感到抱歉,尤其是'excursions-ru(即我忘记了结尾')。
【解决方案3】:

请尝试下面的代码

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

【讨论】:

  • 感谢您的回复。 'our-tours-ru' 页面在哪里?我在子主题的 function.php 中添加了您的代码。它不起作用。
【解决方案4】:

要从特定页面中删除特定类别,请传递页面 id 代替 YOUR_PAGE_ID ,并将以下代码放入您当前的主题 functions.php 文件中。

 add_filter( 'get_terms', 'exclude_category_from_specific_page', 10, 3 );
    function exclude_category_from_specific_page( $terms, $taxonomies, $args ) {
        $new_terms = array();
        // if a product category and on a page
        if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_page('YOUR_PAGE_ID')) {
            foreach ( $terms as $key => $term ) {
                // Enter the name of the category you want to exclude 
                if ( ! in_array( $term->slug, array( 'excursions-ru' ) ) ) {
                    $new_terms[] = $term;
                }
            }
            $terms = $new_terms;
        }
        return $terms;
    }

要从特定类别中删除产品,请使用以下代码

/** * 此代码应添加到您的主题的functions.php **/

add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
    if ( ! $q->is_main_query() ) return;
    if ( ! is_page('YOUR_PAGE_ID') ) return;
    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_cat',
        'field' => 'excursions-ru',
        'terms' => array( 'excursions ru' ),
        'operator' => 'NOT IN'
    )));
    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
}

希望对你有所帮助。 谢谢

【讨论】:

  • 也谢谢你,但它对我和 LoicTheAztec 的建议都不起作用。添加在孩子的function.php中。测试了几次。
猜你喜欢
  • 2018-07-07
  • 2018-11-09
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 2014-04-06
相关资源
最近更新 更多