如何在woocommerce上从标签云中删除空产品标签

问题描述

我看到了许多与此类似的帖子,但没有一篇专门解决我遇到的问题。

我需要创建一个允许我从woocommerce网站上的标签云中删除空标签归档页面的功能,以便它们不会引导用户进入空页面。

我发现的唯一允许删除整个标签云的代码是:

add_action( 'widgets_init','misha_remove_product_tag_cloud_widget' );
 
function misha_remove_product_tag_cloud_widget(){
    unregister_widget('WC_Widget_Product_Tag_Cloud');
}

我相信这可以与类似以下代码的代码结合使用,该代码允许从各个位置删除所有未使用的类别:

add_filter( 'wp_get_nav_menu_items','nav_remove_empty_category_menu_item',10,3 );
function nav_remove_empty_category_menu_item ( $items,$menu,$args ) {
    global $wpdb;
    $nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
    foreach ( $items as $key => $item ) {
        if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id,$nopost ) ) ) {
            unset( $items[$key] );
        }
    }
    return $items;
}

我不确定如何将两者一起使用,但是最终结果应允许将所有未使用的产品标签档案页面从标签云中隐藏起来,直到再次使用它们或将其完全从网站中删除为止,但始终会阻止用户访问空标签页!

解决方法

通常默认情况下,WooCommerce产品标签云小部件会隐藏空标签……如果不是这种情况,则可以使用:

add_filter('woocommerce_product_tag_cloud_widget_args','hide_empty_terms_from_product_tag_cloud_widget');
function hide_empty_terms_from_product_tag_cloud_widget( $args ) {
    $args['hide_empty'] = true; // Empty tags not visible
    return $args;
}

要在WooCommerce产品标签云小部件中显示空的产品标签,请使用:

add_filter('woocommerce_product_tag_cloud_widget_args','show_empty_terms_from_product_tag_cloud_widget');
function show_empty_terms_from_product_tag_cloud_widget( $args ) {
    $args['hide_empty'] = false; // Empty tags are visible
    return $args;
}

请参见wp_tag_cloud() used by WooCommerce tag Cloud Widgetget_terms()函数。

,

此问题在此处得到了更深入的回答:WooCommerce - How to hide product tags in the tag cloud when it has no 'in stock' products

用作最终解决方案的代码如下:

/* TURN PRODUCT TAG CLOUD INTO ALPHABETICAL LIST WITH TAG TOTALS COUNT VISIBLE */

function woocommerce_product_tag_cloud_widget_filter($args) {
    $args = array(
        'smallest' => 14,'largest' => 14,'format' => 'list','taxonomy' => 'product_tag','unit' => 'px','show_count' => 1,'number' => 0,);

    echo "<div style='padding-left: 20px; min-height: 50px; max-height: 400px; overflow: auto;'>";
    return $args;
    echo "</div>";
}

add_filter('woocommerce_product_tag_cloud_widget_args','woocommerce_product_tag_cloud_widget_filter');

/* HIDE PRODUCT TAG ARCHIVE PAGES WHEN THEY HAVE NO 'IN STOCK' PRODUCTS */

function hide_empty_tags( $terms,$taxonomies) {
    $new_terms = array();
    
    if ( in_array( 'product_tag',$taxonomies ) && ! is_admin() ) {
        foreach ( $terms as $key => $term ) {
            if ($term->count >0){
                $new_terms[] = $term;
            }
        }
        $terms = $new_terms;
    }
    return $terms;
}

add_filter( 'get_terms','hide_empty_tags',10,3 );

/* REDIRECTS TO SHOP IF THERE ARE NO 'IN STOCK' PRODUCTS IN THE PRODUCT TAG ARCHIVE PAGE */

function redirect_to_shop(){
    global $wp_query;

    if( is_woocommerce() && $wp_query->post_count == 0 ){
        the_post();
    $post_url = "/shop";
    wp_safe_redirect($post_url,302 );
    exit;
    }
} 

add_action('template_redirect','redirect_to_shop');

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...