首页 文章

在档案类别页面和单个产品页面上显示类别菜单

提问于
浏览
2

我的WooCommerce商店有两个主要类别:

cat1
- subcat1
- subcat2

cat2
- subcatA
- subcatB

我已经制作了这两个分支的菜单,它们显示在类别页面上 . 为了便于导航,我还想向他们展示 on single product pages .

我的代码(位于woocommerce.php)是:

<?php 
if ( is_tax( 'product_cat', array(14,18,19,20,21,22,23,24)) OR is_single() ) {
   wp_nav_menu( array( 'theme_location' => 'cat1' ) ); 
   }
   elseif ( is_tax( 'product_cat', array(15,16,17,20)) OR is_single() ) {
   wp_nav_menu( array( 'theme_location' => 'cat2' ) ); 
   }
?>

这适用于类别页面,但 not for the single product pages .

如何分配cat1和cat2的所有单个产品并显示指定的菜单?

谢谢

1 回答

  • 3

    条件标签is_tax()检查 custom taxonomy archive page is being displayed . 因此,如您所见,不适用于单个产品页面 .

    您应该使用has_term()条件函数,它将以类似的方式为您的单个产品页面工作 .

    所以你的代码将是:

    <?php
    
    $cats1 = array(14,18,19,20,21,22,23,24);
    $cats2 = array(15,16,17,20);
    $tax = 'product_cat';
    
    if ( is_tax( $tax, $cats1 ) || has_term( $cats1, $tax ) )
       wp_nav_menu( array( 'theme_location' => 'cat1' ) ); 
    elseif ( is_tax( $tax, $cats2 ) || has_term( $cats2, $tax ) )
       wp_nav_menu( array( 'theme_location' => 'cat2' ) ); 
    
    ?>
    

    这应该像您期望的那样对单个产品页面和存档页面起作用 .

相关问题