繁体   English   中英

删除除一页之外的所有页面上的标签

[英]delete tabs in on all pages except one

我想删除我们商店中的一个显式标签。 一般也没问题。 但是:我想将其归为一类(ID = 15)

我发现此代码可以删除特殊页面上的选项卡

现在需要改变什么?

 add_filter( 'woocommerce_product_tabs', 'conditionaly_removing_product_tabs', 99 );
function conditionaly_removing_product_tabs( $tabs ) {
// Get the global product object
global $product;
// Get the current product ID
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Define HERE your targeted categories (Ids, slugs or names) <=== <=== <===
$product_cats = array( 'clothing', 'posters' );
// If the current product have the same ID than one of the defined IDs in your array,…
// we remove the tab.
if( has_term( $product_cats, 'product_cat', $product_id ) ){
// KEEP BELOW ONLY THE TABS YOU NEED TO REMOVE <=== <=== <=== <===
unset( $tabs['description'] ); // (Description tab)
unset( $tabs['reviews'] ); // (Reviews tab)
unset( $tabs['additional_information'] ); // (Additional information tab)
}
return $tabs;
}```

好的,我不是 woocommerce 专家,也没有使用过它。 但是,了解 wordpress 以及 获取术语的工作原理……您可以尝试以下操作。

    add_filter( 'woocommerce_product_tabs', 'conditionaly_removing_product_tabs', 99 );
 function conditionaly_removing_product_tabs( $tabs ) {
    // Get the global product object
    global $product;
    // Get the current product ID
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    // Define HERE your targeted categories (Ids, slugs or names) <=== <=== <===
    // $product_cats = array( 'clothing', 'posters' );

    // lets get all terms and then programatically exclude certain categories
    $terms = get_terms( 'product_cat', array(
                    'orderby' => 'name',
                    'order'   => 'ASC',
                    'exclude' => array( 77, 71 ), // dont forget to include ids for the categories
    ));

    // Loop over the tabs and remove them from the categories
    array_map(function($term) use ($product_id, $tabs){
        if ( has_term($term, 'product_cat', $product_id)){
            unset( $tabs['description'] ); // (Description tab)
            unset( $tabs['reviews'] ); // (Reviews tab)
            unset( $tabs['additional_information'] ); // (Additional information tab)
        }
    }, $terms);

  return $tabs;

}

感谢您的回答! 但它不起作用......也许我们必须在这里有所不同......

我在functions.php 中有一个代码,我用它添加了一个自定义选项卡。 (这也是将来在某个产品类别中应该只显示“ONLY”的选项卡。

//Preisvorschlag Produkt-Tab hinzufügen
add_filter( 'woocommerce_product_tabs', 'wp_bibel_de_add_woocommerce_product_tabs' );

function wp_bibel_de_add_woocommerce_product_tabs( $tabs ) {
    $tabs['wp_bibel_de_custom_tab'] = array(
        'title'     => __( 'Requests' ),
        'priority'  => 50,
        'callback'  => 'wp_bibel_de_new_product_tab_callback'
    );

    return $tabs;
}

应显示此选项卡的类别 ID 为:228

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM