doqrt26664 2015-05-28 15:38
浏览 61
已采纳

WooCommerce - 检查品牌页面

I'm using WooCommerce on our site along with their Brands add-on to be able to group products together by brand - I know WooCommerce has it's own built in selection of hooks and filters but I want to write a function that checks if I'm on a Brand page or not because as far as I can tell it piggybacks WooCommerce post types to display the information.

I have used get_post_type() on both a normal WooCommerce category and product page, and also when I'm on a Brand page but all three come up with the custom post type of product. I know that the Brands plug-in uses a custom taxonomy of product_brand somewhere but I'm not sure how I can write a query to check if the page I'm on has this taxonomy.

If I run the following query…

$terms = get_terms( 'product_brand');

print_r($terms);

I get all of the brands that we have setup showing on the page.

Can anyone point me in the right direction?

  • 写回答

1条回答 默认 最新

  • dongyipa0028 2015-06-02 09:44
    关注

    I managed to find a solution to this problem using the is_tax() function that Wordpress provides for using on custom taxonomy archives.

    Here's my code to check if I'm on a brand page or not. Have tested and appears to be working fine.

    function is_brand_page_check() {
        if (is_tax( 'product_brand' )) {
            echo 'This is a product brand page';
        } else {
            echo 'This is NOT a product brand page';
        }
    }
    add_action('woocommerce_before_shop_loop', 'is_brand_page_check');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含