Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从woocommerce上的标签云中删除空产品标签_Php_Wordpress_Woocommerce_Widget_Taxonomy Terms - Fatal编程技术网

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

Php 如何从woocommerce上的标签云中删除空产品标签,php,wordpress,woocommerce,widget,taxonomy-terms,Php,Wordpress,Woocommerce,Widget,Taxonomy Terms,我看过很多类似的帖子,但没有一篇专门解决我的问题 我需要创建一个功能,允许我从我的woocommerce网站上的标签云中删除空标签归档页面,这样它们就不会引导用户进入空页面 我发现的唯一允许删除整个标记云的代码是: add_action( 'widgets_init', 'misha_remove_product_tag_cloud_widget' ); function misha_remove_product_tag_cloud_widget(){ unregister_widg

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

我需要创建一个功能,允许我从我的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;
}

请参阅()和函数。

通常默认情况下,空标签会从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;
}

参见()和函数。

这个问题在这里得到了更深入的回答:

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

/* 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');
/*将产品标签云转换为字母列表,标签总数可见*/
功能商业\产品\标签\云\小部件\过滤器($args){
$args=数组(
“最小”=>14,
“最大”=>14,
'格式'=>'列表',
“分类法”=>“产品标签”,
'单位'=>'像素',
“显示计数”=>1,
“数字”=>0,
);
回声“;
返回$args;
回声“;
}
添加过滤器(“woocommerce_产品_标签_云_小部件_参数”,“woocommerce_产品_标签_云_小部件_过滤器”);
/*当没有“库存”产品时,隐藏产品标签存档页*/
函数隐藏\u空\u标记($terms,$taxonomies){
$new_terms=array();
if(在_数组('product_tag',$taxonomies)&&!is_admin()中){
foreach($key=>$term的术语){
如果($term->count>0){
$new_terms[]=$term;
}
}
$terms=$new_术语;
}
返回$terms;
}
添加过滤器('get_terms','hide_empty_tags',10,3);
/*如果产品标签存档页面中没有“库存”产品,则重定向到商店*/
函数将_重定向到_shop(){
全局$wp_查询;
如果(is_woocommerce()&&$wp_query->post_count==0){
_post();
$post_url=“/shop”;
wp_安全重定向($post_url,302);
出口
}
} 
添加操作(“模板重定向”,“重定向到车间”);

这个问题在这里得到了更深入的回答:

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

/* 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');
/*将产品标签云转换为字母列表,标签总数可见*/
功能商业\产品\标签\云\小部件\过滤器($args){
$args=数组(
“最小”=>14,
“最大”=>14,
'格式'=>'列表',
“分类法”=>“产品标签”,
'单位'=>'像素',
“显示计数”=>1,
“数字”=>0,
);
回声“;
返回$args;
回声“;
}
添加过滤器(“woocommerce_产品_标签_云_小部件_参数”,“woocommerce_产品_标签_云_小部件_过滤器”);
/*当没有“库存”产品时,隐藏产品标签存档页*/
函数隐藏\u空\u标记($terms,$taxonomies){
$new_terms=array();
if(在_数组('product_tag',$taxonomies)&&!is_admin()中){
foreach($key=>$term的术语){
如果($term->count>0){
$new_terms[]=$term;
}
}
$terms=$new_术语;
}
返回$terms;
}
添加过滤器('get_terms','hide_empty_tags',10,3);
/*如果产品标签存档页面中没有“库存”产品,则重定向到商店*/
函数将_重定向到_shop(){
全局$wp_查询;
如果(is_woocommerce()&&$wp_query->post_count==0){
_post();
$post_url=“/shop”;
wp_安全重定向($post_url,302);
出口
}
} 
添加操作(“模板重定向”,“重定向到车间”);

不幸的是,我的问题可能措辞不当。虽然我理解这将从标签云中隐藏空标签,因为标签不会变为空,而是有零库存的产品(因此隐藏在网站上),标签云仍然显示这些产品。我需要标签变得不可见,不仅是当它们完全空的时候,而且当它们只包含缺货的产品时。。。有没有办法把这张支票写进你的代码里?不幸的是,我的问题可能写得不好。虽然我理解这将从标签云中隐藏空标签,因为标签不会变为空,而是有零库存的产品(因此隐藏在网站上),标签云仍然显示这些产品。我需要标签变得不可见,不仅是当它们完全空的时候,而且当它们只包含缺货的产品时。。。有没有办法把这张支票写进你的代码里?