2 WooCommerce 商店的布局 [英] 2 Layouts for a WooCommerce shop

查看:36
本文介绍了2 WooCommerce 商店的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将类别命名为Collectie"和Shop",我想要的是两个类别的子项的不同布局.

I have to categories named "Collectie" and "Shop", what I want is different layouts for the children of both categories.

我已经用这样的 template_include 函数试过了:

I already tried this with the template_include function like this:

function lab5_template_include( $template ) {
    if ( category_has_children('collectie')) {
        $template = get_stylesheet_directory() . '/woocommerce/archive-product-collectie.php';
    }elseif ( is_product_category('shop')) {
        $template = get_stylesheet_directory() . '/woocommerce/archive-product-shop.php';
    } 
    return $template;
}

我该怎么做?

编辑

我用 https://wordpress.org/support/topic/different-page-layouts-for-category-vs-subcategory

我在 taxonomy-product_cat.php 中添加了下一行

i added the next lines to taxonomy-product_cat.php

// We need to get the top-level category so we know which template to load.
$get_cat = $wp_query->query['product_cat'];

// Split
$all_the_cats = explode('/', $get_cat);

// How many categories are there?
$cat_count = count($all_the_cats);

//
// All the cats say meow!
//

// Define the parent
$parent_cat = $all_the_cats[0];

// Collectie
if ( $parent_cat == 'collectie' ) woocommerce_get_template( 'archive-product-collectie.php' );

// Shop
elseif ( $parent_cat == 'shop' ) woocommerce_get_template( 'archive-product.php' );

推荐答案

我认为最好运行 while 循环来确定父级类别名称.然后,您可以将其与要以不同方式显示的不同父类别进行匹配.

I think you'd be better off running a while loop to determine the parent level category name. Then you can match it against the different parent categories you want to display differently.

edit lab5_template_include() 应该使用 is_product_category() 而不是 is_product_taxonomy() 在产品上返回 true类别标签存档.

edit lab5_template_include() should use is_product_category() instead of is_product_taxonomy() which returns true on product category and tag archives.

function lab5_template_include( $template ) {

    if( is_product_category()){ 

        $slug = get_query_var('product_cat');

        $cat = get_term_by( 'slug', $slug, 'product_cat' ); 

        $catParent = so_top_product_category_slug($cat);

        // locate the new templates
        if ( 'collectie' == $catParent ) {
            $new_template = locate_template( array( 'woocommerce/archive-product-collectie.php' ) );
        } elseif ( 'shop' == $catParent ) ) {
            $new_template = locate_template( array( 'woocommerce/archive-product-shop.php' ) );
        } 

        // set the new template if found
        if ( '' != $new_template ) {
            $template = $new_template ;
        }

    }

    return $template;
}
add_filter('template_include', 'lab5_template_include', 20 );

edit 修复了错误并提高了 so_top_product_category_slug() 的效率.现在测试和工作.

edit bug fixes and improved efficiency of so_top_product_category_slug(). Now tested and working.

// get the top-level parent product category from a term object or term slug
function so_top_product_category_slug($cat) {
    if( is_object( $cat ) ){
        $catid = $cat->term_id;
    } else {
        $cat = get_term_by( 'slug', $cat, 'product_cat' );
        $catid = $cat->term_id;
    }
    $parentId = $cat->parent;
    $parentSlug = $cat->slug;
    while ($parentId > 0) {
        $cat = get_term_by( 'id', $parentId, 'product_cat' );
        $parentId = $cat->parent; // assign parent ID (if exists) to $catid
        $parentSlug = $cat->slug;
    }
    return $parentSlug;
}

这篇关于2 WooCommerce 商店的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆