如何从woocommerce的标签云中删除空的产品标签 [英] How to remove empty product tags from the tag cloud on woocommerce

查看:18
本文介绍了如何从woocommerce的标签云中删除空的产品标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多类似的帖子,但没有一个专门解决我遇到的问题.

I have seen a number of similar posts to this, but not one that specifically solves the issue I am having.

我需要创建一个函数,允许我从 woocommerce 网站上的标签云中删除空标签存档页面,这样它们就不会将用户引导至空页面.

I need to create a function that allows me to remove empty tag archive pages from the tag cloud on my woocommerce site so they do not lead users onto empty pages.

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

The only code I have found that allows removal of the entire tag cloud is:

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

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

I believe this could be used in conjunction with something like this code which allows for the removal of all unused categories from various locations:

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;
}

我不确定如何将两者结合使用,但最终结果应该允许任何未使用的产品标签存档页面从标签云中隐藏,直到它们再次使用或从网站上完全删除 - 但始终阻止用户从访问空标签页!

I am unsure how to work the two together, but the end result should allow for any unused product tag archive pages to be hidden from the tag cloud until they are either used again or removed entirely from the website - but always preventing users from accessing empty tag pages!

推荐答案

这里更深入地回答了这个问题:WooCommerce - 如何在没有库存"产品时在标签云中隐藏产品标签

This question was answered in more depth over here: WooCommerce - How to hide product tags in the tag cloud when it has no 'in stock' products

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

The code used as the final solution is as follows:

/* 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');

这篇关于如何从woocommerce的标签云中删除空的产品标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆