第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取 Woocommerce 檔案中當前產(chǎn)品類別的子類別

獲取 Woocommerce 檔案中當前產(chǎn)品類別的子類別

PHP
狐的傳說 2021-12-24 10:18:23
我正在嘗試在 Woocommerce 中顯示當前類別下的子類別(不是子類別等),比如這個網(wǎng)站:http ://www.qs-adhesivos.es/app/productos/productos.asp?idioma=en例如,建筑是類別,密封劑和粘合劑、防水材料、聚氨酯泡沫……是子類別。密封劑和膠粘劑是類別,而醋酸硅酮密封劑、中性硅酮密封劑、丙烯酸密封劑……是子類別……在我的子主題下的 woocommerce 文件夾中已經(jīng)有一個 archive-product.php。已經(jīng)嘗試了一些代碼并且它適用,但這不是我想要的。
查看完整描述

1 回答

?
眼眸繁星

TA貢獻1873條經(jīng)驗 獲得超9個贊

以下代碼將顯示產(chǎn)品類別存檔頁面的當前產(chǎn)品類別的格式化鏈接產(chǎn)品子類別:


if ( is_product_category() ) {


    $term_id  = get_queried_object_id();

    $taxonomy = 'product_cat';


    // Get subcategories of the current category

    $terms    = get_terms([

        'taxonomy'    => $taxonomy,

        'hide_empty'  => true,

        'parent'      => get_queried_object_id()

    ]);


    $output = '<ul class="subcategories-list">';


    // Loop through product subcategories WP_Term Objects

    foreach ( $terms as $term ) {

        $term_link = get_term_link( $term, $taxonomy );


        $output .= '<li class="'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></li>';

    }


    echo $output . '</ul>';

}

測試和工作。


使用示例:


1) 您可以直接在archive-product.php模板文件中使用此代碼。


2)可以在一個功能嵌入的代碼,替換最后一行echo $output . '</ul>';通過return $output . '</ul>';,對于簡碼,總是返回顯示。


3)您可以使用以下動作掛鉤嵌入代碼woocommerce_archive_description:


// Displaying the subcategories after category title

add_action('woocommerce_archive_description', 'display_subcategories_list', 5 ); 

function display_subcategories_list() {

    if ( is_product_category() ) {


        $term_id  = get_queried_object_id();

        $taxonomy = 'product_cat';


        // Get subcategories of the current category

        $terms    = get_terms([

            'taxonomy'    => $taxonomy,

            'hide_empty'  => true,

            'parent'      => $term_id

        ]);


        echo '<ul class="subcategories-list">';


        // Loop through product subcategories WP_Term Objects

        foreach ( $terms as $term ) {

            $term_link = get_term_link( $term, $taxonomy );


            echo '<li class="'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></li>';

        }


        echo '</ul>';

    }

}

代碼位于活動子主題(或活動主題)的 functions.php 文件中。測試和工作。


要將其顯示在類別描述之后,請將掛鉤優(yōu)先級從更改5為20in:


add_action('woocommerce_archive_description', 'display_subcategories_list', 5 ); 

喜歡:


add_action('woocommerce_archive_description', 'display_subcategories_list', 20 ); 


查看完整回答
反對 回復 2021-12-24
  • 1 回答
  • 0 關注
  • 202 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號