Woocommerce产品/类别循环问题

问题描述 投票:0回答:1

我安装了最新的Woocommerce和最新的Wordpress。我已经安装了网格/列表切换。我的问题从这里开始。因为它将所有类别和产品更改为列表视图

我想要网格视图中的类别

和listView中的产品。

因此,我搜索并得出结论,类别和产品使用相同的开始和结束循环template / loop / loop-start.php“>

可以在那里更改它,但是随后它将所有内容更改为该类。

现在我的解决方案是以下向该页面添加条件标签

但是这有点问题。查看代码

<?php
/**
 * Product Loop Start
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/loop/loop-start.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @package     WooCommerce/Templates
 * @version     3.3.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>
<?php 
if ( is_product_category() ) {

  if ( is_shop() ) {
    echo '<ul class="product-category1">';
  } elseif ( is_product_category( array( 'cd', 'album' ) )) {
    echo '<ul class="product-category2">';
  } else {
    echo '<ul class="products columns-' .esc_attr( wc_get_loop_prop( 'columns' ) );.' ">';
  }

} ?>

<!--<ul class="products columns-<?php //echo esc_attr( wc_get_loop_prop( 'columns' ) ); ?>" id="HELP">-->

现在上面的代码可以正常工作了if_shop语句不起作用,但是is_product_category像超级按钮一样工作,else语句也不起作用

有人可以为此指出正确的方向,几周以来一直在寻找答案,但是随着Woocommerce更改了许多代码,没有什么接近或太旧而无法使用。

php wordpress woocommerce
1个回答
0
投票

好几天后,现在有人帮助了,我开始工作了。请记住复制woocommerce / template / loop / loop-start.php和loop-end.php到您的主题/woocommerce/loop/loop-start.php和loop-end.php

loop / loop-start.php

/*This is the main cat on homepage or shop page*/
if (is_shop()|| is_front_page()) {
/*this can be any thing you want it to be <ul><div>*/
echo '<div class="yourclass">';
} 
/*this should be your sub categories 1 level down*/
else if ( is_product_category(array('cat1', 'cat2','cat3', 'cat4', 'cat5'))){
/*this can be any thing you want it to be <ul><div>*/
echo '<div class="yourclass">';
}
/*this should be your sub categories 2 level down*/
 else if ( is_product_category(array('subcat1','subcat2'))){
echo '<ul class="products" id="NORMAL">';
}

然后进入loop-end.php

/*This is the main cat on homepage or shop page*/
    if (is_shop()|| is_front_page()) {
    /*this can be any thing you want it to be <ul><div>*/
    echo '</div>';
    } 
    /*this should be your sub categories 1 level down*/
    else if ( is_product_category(array('cat1', 'cat2','cat3', 'cat4', 'cat5'))){
    /*this can be any thing you want it to be <ul><div>*/
    echo '</div>';
    }
    /*this should be your sub categories 2 level down*/
     else if ( is_product_category(array('subcat1','subcat2'))){
    echo '</ul>';
    }

应该有更好的方法来做到这一点,但对我来说,我所能做的一切。我可以在网上找到的钩子曾经是旧的,在Woocommerce上不再可用。

希望这可以帮助某人!

PS:总是备份您编辑的文件,因此,如果您犯了错误,您就可以保留旧的文件[

© www.soinside.com 2019 - 2024. All rights reserved.