【问题标题】:Change header based on parent product category in Woocommerce根据 Woocommerce 中的父产品类别更改标题
【发布时间】:2019-02-04 20:50:53
【问题描述】:

我正在经营一家电子商务商店,其中有两个自定义标题用于商店的不同方面。我想知道是否有可能:

  • 要显示标题 A 的父产品类别 A
  • 要显示标题 B 的父产品类别 B

我只是不知道把它放在我的主题中的什么位置。

这是我的实际代码示例:

if(is_category('category-a-slug')){
    get_header('a');
}elseif(is_category('category-b-slug')){
    get_header('b');
}

感谢任何帮助。

【问题讨论】:

    标签: php wordpress if-statement woocommerce custom-taxonomy


    【解决方案1】:

    要根据父产品类别获取不同的标题,请使用以下代码:

    // For product category archive pages only
    if(is_product_category()){
        // Get the WP_Term object for the current product category
        $queried_obj = get_queried_object();
        $parent_id   = $queried_obj->parent; // The parent term ID
        $taxonomy    = $queried_obj->taxonomy; // The taxonomy
        
        // If a parent term id exist, we get the parent term slug
        if( $parent_id > 0 ){
            $parent_slug = get_term_by( 'term_id', $parent_id, $taxonomy )->slug;
        }
        
        // If a parent slug exist and for parent term slug 'category-a-slug'
        if ( isset($parent_slug) && $parent_slug == 'category-a-slug' ) {
            get_header('a');
        } 
        // If a parent slug exist and for parent term slug 'category-b-slug'
        elseif ( isset($parent_slug) && $parent_slug == 'category-b-slug' ) {
            get_header('b');
        } 
        // Other cases
        else {
            get_header('shop');
        }
    // For other Woocommerce archives types (as shop)
    } else {
        get_header('shop');
    }
    

    它应该可以工作。

    代码继续 Woocommerce 模板 archive-product.php 替换 get_header('shop');

    见:How to override Woocommerce templates via the active theme (documentation)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 2016-12-13
      • 2018-11-06
      • 1970-01-01
      相关资源
      最近更新 更多