[英]Remove categories from widget based on number of products
由於我的商店有很多子類別,因此我試圖從小部件中排除僅包含1種產品的所有類別。
現在,我發現以下代碼按ID排除了貓,但是在這種情況下這樣做會很費時間。 關於如何編輯此代碼以按帖子數排除的任何建議?
add_filter( 'woocommerce_product_categories_widget_args', 'wdm_edit_product_cat_widget_args' ); function wdm_edit_product_cat_widget_args( $cat_args ) {
$cat_args['exclude'] = array('10387');
return $cat_args;}
我想到的一種有趣的方式是使用您定制的Walker列出類別。 那里有一些選擇,包括:
您可以檢查某個項目是否有1個產品,然后可以添加條件標簽並阻止其顯示。
您可以在li類中添加該項目中的產品數量,並使用css隱藏具有某些類(.cat-count-1)的項目。
我個人喜歡第二種方法,因為它允許您以后對項目進行任何處理。
1-在您的主題的functions.php
文件中添加以下代碼:
add_filter('woocommerce_product_categories_widget_args', 'use_different_walker_with_counts');
function use_different_walker_with_counts($args){
include_once 'wc_walker_with_counter_class.php';
$args['walker'] = new WC_Product_Cat_List_Counter_Walker;
return $args;
}
這告訴wordpress使用另一個定制的助行器。
2-在主題文件夾名稱wc_walker_with_counter_class.php
添加一個新文件,其內容如下:
<?php
class WC_Product_Cat_List_Counter_Walker extends Walker {
/**
* What the class handles.
*
* @var string
*/
public $tree_type = 'product_cat';
/**
* DB fields to use.
*
* @var array
*/
public $db_fields = array(
'parent' => 'parent',
'id' => 'term_id',
'slug' => 'slug'
);
/**
* Starts the list before the elements are added.
*
* @see Walker::start_lvl()
* @since 2.1.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of category. Used for tab indentation.
* @param array $args Will only append content if style argument value is 'list'.
*/
public function start_lvl( &$output, $depth = 0, $args = array() ) {
if ( 'list' != $args['style'] )
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent<ul class='children'>\n";
}
/**
* Ends the list of after the elements are added.
*
* @see Walker::end_lvl()
* @since 2.1.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of category. Used for tab indentation.
* @param array $args Will only append content if style argument value is 'list'.
*/
public function end_lvl( &$output, $depth = 0, $args = array() ) {
if ( 'list' != $args['style'] )
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
/**
* Start the element output.
*
* @see Walker::start_el()
* @since 2.1.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of category in reference to parents.
* @param integer $current_object_id
*/
public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) {
$output .= '<li class="cat-item cat-item-' . $cat->term_id . ' cat-count-' . $cat->count;
if ( $args['current_category'] == $cat->term_id ) {
$output .= ' current-cat';
}
if ( $args['has_children'] && $args['hierarchical'] ) {
$output .= ' cat-parent';
}
if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat->term_id, $args['current_category_ancestors'] ) ) {
$output .= ' current-cat-parent';
}
$output .= '"><a href="' . get_term_link( (int) $cat->term_id, $this->tree_type ) . '">' . _x( $cat->name, 'product category name', 'woocommerce' ) . '</a>';
if ( $args['show_count'] ) {
$output .= ' <span class="count">(' . $cat->count . ')</span>';
}
}
/**
* Ends the element output, if needed.
*
* @see Walker::end_el()
* @since 2.1.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of category. Not used.
* @param array $args Only uses 'list' for whether should append to output.
*/
public function end_el( &$output, $cat, $depth = 0, $args = array() ) {
$output .= "</li>\n";
}
/**
* Traverse elements to create list from elements.
*
* Display one element if the element doesn't have any children otherwise,
* display the element and its children. Will only traverse up to the max.
* depth and no ignore elements under that depth. It is possible to set the.
* max depth to include all depths, see walk() method.
*
* This method shouldn't be called directly, use the walk() method instead.
*
* @since 2.5.0
*
* @param object $element Data object
* @param array $children_elements List of elements to continue traversing.
* @param int $max_depth Max depth to traverse.
* @param int $depth Depth of current element.
* @param array $args
* @param string $output Passed by reference. Used to append additional content.
* @return null Null on failure with no changes to parameters.
*/
public function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {
if ( ! $element || ( 0 === $element->count && ! empty( $args[0]['hide_empty'] ) ) ) {
return;
}
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}
3-然后,您可以繼續在樣式文件(通常是style.css
。 在這種情況下,我們要隱藏計數為1的項目,因此我們添加:
.cat-count-1 {
display: none;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.