【问题标题】:Woocommerce archive-product.php template function broken after update 3.3.0 / 3.3.1Woocommerce archive-product.php 模板功能在更新 3.3.0 / 3.3.1 后损坏
【发布时间】:2018-02-08 11:17:15
【问题描述】:

多年来,我一直在使用下面的代码,没有任何问题,直到我升级到 Woocommerce 版本 3.1.1。

该函数只是将默认的 archive-product.php 更改为基于定义的 Woocommerce 类别 slug 的替代模板。

我已经阅读了更改日志,我认为有几件事可能与我的问题有关,但我不确定,因此我在这里联系 :)

更新日志给我的相关注释似乎是:

修复 - 添加了 woocommerce_output_product_categories 来替换 woocommerce_product_subcategories 功能以防止过时的主题 模板文件来自输出商店和类别上的类别 页面出错。

但是,在之前的 3.3.0 版本中对 Woocommerce 的默认布局/主题进行了许多更改,这也可能存在问题。

对此的任何帮助或指导都会很棒。

add_filter( 'template_include', 'wpse138858_woocommerce_category_archive_template' );

function wpse138858_woocommerce_category_archive_template( $original_template ) {

    if ( is_product_category( array( 'cat-1', 'cat-2' ) ) )
    {
        return get_stylesheet_directory().'/woocommerce/archive-product_no_sidebar.php';
    }
    elseif ( is_product_category( array( 'cat-3', 'cat-4' ) ) )
    {
        return get_stylesheet_directory().'/woocommerce/archive-product_sidebar.php';
    } 
    elseif ( is_product_category( array( 'cat-5', 'cat-6' ) ) )
    {
        return get_stylesheet_directory().'/woocommerce/archive-product_clubs_page.php';
    } 
    else 
    {
        return $original_template;
    }
}

编辑 - 函数中存在不应该存在的错误,我的错,抱歉...我直接从现场站点复制了代码,只是减少了类别,所以它更具可读性

【问题讨论】:

  • 抱歉,这是我复制代码时的错误,我已将 } 添加回其中,这就是它在我的实时网站上的样子,问题仍然存在
  • 你能告诉我们究竟是什么坏了?它显示或不显示什么?
  • 在更新到 Woocommerce 3.3.0 - 3.3.1 之前,此函数将根据数组中提供的类别 slug 加载不同的 archive-product.php 模板文件。更新后,该功能停止工作,并且始终加载默认的 archive-product.php
  • @Jonnygogo Woocommerce 3.3.1 是非常新的,仍然存在一些错误。此外,许多主题和插件并未全部更新以供使用。所以版本 3.3.1 还没有准备好用于生产网站......你应该 roll back in 3.2.6 stable version 并等待一段时间,因为在 StackOverFlow 和所有互联网相关线程上与 Woocommerce 3.3+ 挂钩 template_include 周围有很多问题......
  • 我在 3.3.0 版中遇到了同样的问题。我回滚到版本 3.2.6,template_include() 现在工作正常。谢谢@LoicTheAztec

标签: php wordpress function templates woocommerce


【解决方案1】:

安装插件 WP Rollback,然后在 woocommerce 上的已安装插件菜单中单击回滚。必须这样做。

【讨论】:

    【解决方案2】:

    else 语句在您的 elseif 语句中。你必须像这样把它放在后面:

    add_filter( 'template_include', 'wpse138858_woocommerce_category_archive_template' );
    function wpse138858_woocommerce_category_archive_template( $original_template ) {
    
        // If cat 1,2,3 return template archive product set 1
        if(is_product_category(array('cat-1','cat-2','cat-3'))) {
    
            return get_stylesheet_directory().'/woocommerce/archive-product_set_1.php';
    
        // If cat 4,5,6 return template archive product set 2
        }elseif(is_product_category(array('cat-4','cat-5','cat-6'))) {
    
            return get_stylesheet_directory().'/woocommerce/archive-product_set_2.php';
    
        // Else return original template
        }else {
    
            return $original_template;
        }
    }
    

    【讨论】:

    • 抱歉,这是我复制代码时的错误,我已将 } 添加回其中,这就是它在我的实时网站上的样子,问题仍然存在
    • 你还有一个“}”个备用
    • 嗨,Alex,谢谢,我已经从我的现场网站重新复制了代码,这次只删除了一些类别。因为它在更新之前正在工作,我假设这是没有错误的?
    猜你喜欢
    • 1970-01-01
    • 2020-09-15
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2013-04-27
    相关资源
    最近更新 更多