我通常能够自己解决我的每一个工作流程需求,但事实证明这有点棘手。
问题是,我需要根据我所在的类别从“产品类别”小部件中隐藏某些类别。
示例: 当我进入A类时,我想显示A,B,C,D类,但不显示E。 但是,当我属于B类时,我也要显示A,B,C,D和E类。 然后,假设我属于C类,并且只想显示C类和D类。
我找到了以下代码
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
$cat_args['exclude'] = array('15');
return $cat_args;
}
我要用此代码做的事情是添加多行,以便定义例如当我打开类别ID 1时不显示ID 2、3、4。然后,下一行将定义不显示ID 2的ID,然后显示ID 4,以此类推。
/ *编辑 一个出色的stackoverflow用户通过创建您可以在下面的帖子中看到的代码来帮助我。但是,正如我们发现的那样,我上面发布的示例仅适用于内置的WP Product Categories Widget。
这使我发现我使用的Product Categories Widget是使用我使用的主题定制构建的,显然,它使用以下代码来工作:
if ( !function_exists( 'getbowtied_megamenu_output_shop_icons' )):
/**
* Build the layout for the "Shop Icons" type megamenu
*
* @param int $theID id of the menu item
*
* @return html
*/
function getbowtied_megamenu_output_shop_icons( $theID, $cat= false) {
if ( !GETBOWTIED_WOOCOMMERCE_IS_ACTIVE ) return;
$cat_list = GBT_Opt::getOption('product_categories_icons_megamenu_' . $theID );
ob_start();
if ($cat !== true):
$args= array( 'taxonomy' => 'product_cat','hide_empty' => 0, 'menu_order' => 'asc', 'parent' =>0, 'include' => $cat_list );
else:
$args= array( 'taxonomy' => 'product_cat','hide_empty' => 0, 'menu_order' => 'asc', 'parent' =>$theID, 'include' => $cat_list );
endif;
$cats = get_terms( $args );
if ( is_array($cat_list)):
$unsorted = array();
$sorted = array();
foreach ($cats as $v) {
$unsorted[$v->term_id] = $v;
}
foreach ($cat_list as $v) {
if (isset($unsorted[apply_filters( 'wpml_object_id', $v, 'category', TRUE)]))
$sorted[] = $unsorted[apply_filters( 'wpml_object_id', $v, 'category', TRUE)];
}
else:
$sorted = $cats;
$sorted = array_slice($cats, 0, 8);
endif;
echo '<div class="megamenu_icon_list">';
foreach( $sorted as $cat ) {
$icon_type = get_term_meta( $cat->term_id, 'getbowtied_icon_type', true );
if ( $icon_type == 'custom_icon' ) {
$thumbnail_id = get_term_meta( $cat->term_id, 'icon_img_id', true );
if ($thumbnail_id)
$icon = wp_get_attachment_thumb_url( $thumbnail_id );
else
$icon = wc_placeholder_img_src();
// Prevent esc_url from breaking spaces in urls for image embeds
// Ref: https://core.trac.wordpress.org/ticket/23605
$icon = str_replace( ' ', '%20', $icon );
echo '<a href="'.esc_url( get_term_link( $cat->term_id ) ).'"><img src="'. $icon .'" alt="'. $cat->name .'" /><span>'. $cat->name .'</span></a>';
} else {
$icon = get_term_meta( $cat->term_id, 'icon_id', true );
if (!$icon) {
$icon = 'thehanger-icons-alignment_align-all-1';
}
echo '<a href="'.esc_url( get_term_link( $cat->term_id ) ).'"><i class="'. $icon .'"></i><span>'. $cat->name .'</span></a>';
}
}
echo '</div>';
$output = ob_get_contents();
ob_end_clean();
return $output;
}
endif;
* /
请,有人能为我指出正确的方向吗?
谢谢。
答案 0 :(得分:2)
有几种方法,例如以下方法
function woo_product_cat_widget_args( $cat_args ) {
if ( is_product_category() ) {
// Get current category id
$current_cat_id = get_queried_object_id();
// Category id = ?, exclude ids ??
$cat_id_1_exclude_ids = array( 1, 2, 3, 4 );
$cat_id_2_exclude_ids = array( 1, 2, 3, 4, 5 );
$cat_id_15_exclude_ids = array( 16, 18 );
if ( !empty( $current_cat_id ) ) {
// Excludes ID's based on current category id
// Extra check that the variable name (array) exists
$exclude_ids = isset( ${'cat_id_' . $current_cat_id . '_exclude_ids'} ) ? ${'cat_id_' . $current_cat_id . '_exclude_ids'} : '';
if ( $exclude_ids ) {
$cat_args['exclude'] = $exclude_ids;
}
}
}
return $cat_args;
}
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args', 10, 1 );
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'woo_product_cat_widget_args', 10, 1 );