【问题标题】:How can I check if I'm at the category overview page in WooCommerce?如何检查我是否在 WooCommerce 的类别概览页面?
【发布时间】:2019-02-02 03:23:45
【问题描述】:

如果我位于 WooCommerce 中所有类别的概览页面,我该如何检查?我在这里尝试过这个功能,但它不起作用:

/**
 * Function to check if it's category overview page
 *
 * @return bool
 */
function is_category_overview() {
    $cat      = get_query_var( 'product_cat' );

    $category = get_term_by( 'slug', $cat, 'product_cat' );


    return ! empty( $category ) && $category->parent !== 0;
}

所以我的意思是我可以看到所有类别的页面,当我点击一个类别时,我会被重定向到 WooCommerce 中的所有产品。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    你可以写成,

    function check_category_overview(){
        $queried_object = get_queried_object();
        $children = get_terms( $queried_object->taxonomy, array(
            'parent'    => $queried_object->term_id,
            'hide_empty' => false
        ) );
    
        if($children) { // check if has subcategories
            // term has children
    
        }
        if($queried_object->parent){
            //check if has a parent category
    
        }
    
        if($queried_object->count){
            //check if has products in this category
    
        }
    }
    

    我在这段代码中提到了 3 个条件,您可以使用适合您要求的一个条件

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2022-11-23
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多