【问题标题】:delete tabs in on all pages except one删除除一个以外的所有页面上的选项卡
【发布时间】:2020-01-31 17:53:24
【问题描述】:

我想删除我们商店中的显式标签。 一般也没有问题。 但是:我想把它归为一类(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;
}```

【问题讨论】:

  • 类别 id = 15 的名称是什么??
  • name="请求"

标签: php wordpress tabs


【解决方案1】:

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

    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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2019-08-11
    • 2017-04-06
    相关资源
    最近更新 更多