wordpress - WooCommerce:从产品类别小部件中隐藏类别
问题描述
我通常能够自己解决我的每一个 WP 需求,但事实证明这有点棘手。
问题是,我需要根据我所在的类别从产品类别小部件中隐藏某些类别。
示例:当我在类别 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。
这使我发现我使用的产品类别小部件是根据我使用的主题定制的,显然,它使用以下代码工作:
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;
*/
拜托,谁能指出我如何处理这项任务的正确方向?
先感谢您。
解决方案
有几种方式,例如以下方式
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 );
推荐阅读
- pvs-studio - 集成 PVS-Studio 和 CMake
- objective-c - 非法的 NSTableView 数据源 - 我做错了什么?
- javascript - 在 Acrobat 表单中使用基于字符数的 javascript 替换字体
- crystal-reports - Crystal Reports - 根据摘要字段进行动态分组
- javascript - 如何同步来自两个不同帐户的两个谷歌日历
- excel - Excel将两个动态范围合并/合并为一个范围
- java - POST 请求的 I/O 错误... java.net.SocketException: Connection reset
- python - 有没有一种有效的迭代方式?
- arrays - VBA - 如何将单列作为输入数组然后输出删除所有奇数的数组
- django - 不同数据库上的 Django 身份验证