从 Entrada 主题中特定页面上的产品类别中排除产品
Exclude products from a product category on a specific page in Entrada theme
我想从 'Our tours' 页面 (slug 'our-tours-ru') 的 woocommerce 类别 'Excursions' (slug 'excursions-ru') 中排除所有 products/tours。这里是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;
}
请为此尝试以下代码
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' );
要从特定页面中删除特定类别,请传递页面 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' );
}
希望对您有所帮助。
谢谢
更新 1 (见下文)
The get_terms
filter hook is not made to filter products, so not convenient in your case.
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 文件。经过测试并有效……它也应该适合您。
You need to be sure that this products are shown on a Wordpress PAGE which slug is 'our-tours-ru', because if not no answer will work for you.
最后一种适用于大多数情况的可能性,使用 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 文件。
(由于现有答案中的 none 适合您,并且您使用的是子主题,因此我发布了这个特定于 Entrada theme which you currently use on your site 的答案。)
"Our Tours" 页面,如我所见,正在使用“列表全宽(三列网格)”模板,在父主题中,它位于 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 );
用于 "Our Tours" 页面上的 主要 产品 section/grid。
因此,从该查询中排除 "Excursions" 类别或从该类别中排除产品的一种简单方法(并且可能有效)是将 entrada/template-parts/grid-threecolumn.php
复制到子主题文件夹(即 entrada-child-picpic-v1_0/template-parts/grid-threecolumn.php
),然后根据需要自定义 $args
数组。对于您的问题,请尝试将此 before/above 添加到 $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
模板部分 的任何其他页面模板)。
我想从 'Our tours' 页面 (slug 'our-tours-ru') 的 woocommerce 类别 'Excursions' (slug 'excursions-ru') 中排除所有 products/tours。这里是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;
}
请为此尝试以下代码
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' );
要从特定页面中删除特定类别,请传递页面 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' );
}
希望对您有所帮助。 谢谢
更新 1 (见下文)
The
get_terms
filter hook is not made to filter products, so not convenient in your case.
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 文件。经过测试并有效……它也应该适合您。
You need to be sure that this products are shown on a Wordpress PAGE which slug is 'our-tours-ru', because if not no answer will work for you.
最后一种适用于大多数情况的可能性,使用 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 文件。
(由于现有答案中的 none 适合您,并且您使用的是子主题,因此我发布了这个特定于 Entrada theme which you currently use on your site 的答案。)
"Our Tours" 页面,如我所见,正在使用“列表全宽(三列网格)”模板,在父主题中,它位于 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 );
用于 "Our Tours" 页面上的 主要 产品 section/grid。
因此,从该查询中排除 "Excursions" 类别或从该类别中排除产品的一种简单方法(并且可能有效)是将 entrada/template-parts/grid-threecolumn.php
复制到子主题文件夹(即 entrada-child-picpic-v1_0/template-parts/grid-threecolumn.php
),然后根据需要自定义 $args
数组。对于您的问题,请尝试将此 before/above 添加到 $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
模板部分 的任何其他页面模板)。