WooCommerce get category slug - Undefined property notice with a function

前端 未结 2 1225
北恋
北恋 2021-01-19 11:29

I use this function to convert the woocommerce category id into a category slug

function woocommerceCategorySlug($id){
    $term = get_term( $id, \'product_c         


        
2条回答
  •  梦毁少年i
    2021-01-19 12:10

    The working solution for this is to use WordPress native function get_term_by() and to transpose it in your code this way:

    function woocommerceCategorySlug( $id ){
        $term = get_term_by('id', $id, 'product_cat', 'ARRAY_A');
        return $term['slug'];       
    }
    

    Reference:

    • Code Reference > Function get_term_by()
    • Can't get category object in some templates of WooCommerce

提交回复
热议问题