首页 文章

从GET请求中获取Woocommerce产品类别名称

提问于
浏览
1

我按类别和自定义meta_key在自定义页面模板上过滤我的Woocommerce产品 . 现在我试图回应产品被过滤的类别名称 .

过滤产品后的网址看起来像

.../online-shop/popular-products/?product-cato=23

23是类别的ID . 我可以使用以下内容获取(或回显)id

<?php echo sanitize_text_field($_GET['product-cato']);?>

Any idea how to get the category name with the available cateogry ID (in this case 23)?

1 回答

  • 1

    您可以尝试使用以下内容:

    // Testing that $_GET['product-cato'] has been requested and that the product category exists.
    if( isset( $_GET['product-cato'] ) && term_exists( intval($_GET['product-cato']), 'product_cat' ) ){
        // Get the corresponding WP_Term object
        $term = get_term( intval($_GET['product-cato']), 'product_cat' );
        //Display the term name for the product category
        echo '<p>' . $term->name . '</p>';
    }
    

    经过测试和工作 .

    您不需要清理产品类别名称,因为它已经按条件和intval()函数进行过滤 . 所以它很安全 .

相关问题