【问题标题】:2 Layouts for a WooCommerce shop2 WooCommerce 商店的布局
【发布时间】:2015-11-09 21:27:57
【问题描述】:

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

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

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 中添加了下一行

// 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' );

【问题讨论】:

    标签: php wordpress woocommerce wordpress-theming


    【解决方案1】:

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

    edit lab5_template_include() 应该使用is_product_category() 而不是is_product_taxonomy(),这会在产品类别标签档案上返回true。

    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() 的效率。现在测试和工作。

    // 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;
    }
    

    【讨论】:

    • 感谢 helga,我会尝试并回复它!
    • 仅供参考-我已更新为使用locate_template。我认为这应该会有所帮助。虽然我还没有测试过so_top_product_category_slug() 函数,但它改编自另一个我知道有效的函数,所以我对它寄予厚望。 :)
    • 感谢您的帮助 helga,我用wordpress.org/support/topic/…的解决方案解决了这个问题
    • 1.你试过我的解决方案吗?它没有用吗? 2.您应该在此处发布解决方案,以便您可以接受它作为答案,尽管链接的支持线程甚至与过滤template_include 无关。这是一个完全不同的问题。
    • 嗨 Helga,上面的解决方案不起作用,非常感谢您的帮助。我在评论中发布的链接适合我对“Collectie”和“Shop”类别使用不同布局的需要。
    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多