Woocommerce为每个类别选择网格或列表视图

时间:2015-11-09 18:39:15

标签: wordpress woocommerce

我希望有可能在每个Woocommerce类别的网格和列表视图之间进行选择。我找到了这个插件:https://nl.wordpress.org/plugins/woocommerce-grid-list-toggle/

但是,该插件适用于购物者选择是否在网格视图或列表视图中显示项目。我真正想要的是能够为后端的每个类别分配视图。

示例:

类别A显示为网格 类别B显示为列表

对此我不知所措。

1 个答案:

答案 0 :(得分:2)

this question类似,您需要过滤template_include。您需要调用自定义存档模板archive-list-view.php并将其保存在主题的woocommerce文件夹中。显然,您可以将其命名为任何名称,您只需调整下面的代码即可。

文件夹结构:

/theme-folder/functions.php
/theme-folder/woocommerce/archive-list-view.php

template_include过滤器上,我们会检查我们是否在nussmylch产品类别的术语存档中。如果是这样,我们将寻找并提供新模板。否则,使用标准模板。

编辑:使用了错误的WooCommerce功能is_product_taxonomy()。需要is_product_category()来检查特定类别。

add_filter( 'template_include', 'so_33615903_custom_category_template', 20 );

function so_33615903_custom_category_template( $template ) {
    // check you are on the taxonomy archive for specific category
    if ( is_product_category( 'nussmylch' )  ) {
        $new_template = locate_template( array( 'woocommerce/archive-list-view.php' ) );
        if ( '' != $new_template ) {
            $template = $new_template ;
        }
    }

    return $template;
}

Working Example

相关问题