【问题标题】:Woocommerce Product/Category loop ProblemWoocommerce 产品/类别循环问题
【发布时间】:2020-05-31 10:49:17
【问题描述】:

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

我想要网格视图中的类别

listView 中的产品。

所以我搜索并发现类别和产品使用相同的开始和结束循环 模板/循环/循环开始.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】:

    好几天后,现在有人帮忙了,我让它工作了。 请记得复制 woocommerce/template/loop/loop-start.php 和 loop-end.php 到你的主题/woocommerce/loop/loop-start.php 和 loop-end.php

    循环/循环启动.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:总是备份你编辑的文件,所以如果你犯了错误,你可以直接上传旧文件

    【讨论】:

      猜你喜欢
      • 2016-09-03
      • 2018-09-06
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 2019-04-25
      • 2018-06-09
      • 2018-12-19
      • 2012-03-16
      相关资源
      最近更新 更多