首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce -如何基于类别创建多个单一产品模板?

问WooCommerce -如何基于类别创建多个单一产品模板?
EN

Stack Overflow用户
提问于 2014-09-11 14:00:27
回答 1查看 11.6K关注 0票数 9

你好,我对woocommerce相当陌生,我的店有四类产品使用相同的单一产品模板。我想添加第五类产品,产品页面布局与已经使用的产品非常不同。

这是文件结构-

  • 主题/woocommerce/单一产品/
  • 主题/woocommerce/单一产品-模拟/
  • theme/woocommerce/single-product-mock/title.php
  • theme/woocommerce/content-single-product-mock.php
  • 主题/woocommerce/single-Product.php

我创建了一个名为内容-单产品-mock.php的文件。使用以下代码在单产品. the中使用

代码语言:javascript
复制
        <?php if (has_term( 'mock', 'product_cat' )) {
            woocommerce_get_template_part( 'content', 'single-product-mock' );
        } else{
         wc_get_template_part( 'content', 'single-product' ); 
        } ?>

对于模拟类别,它重定向到内容-单一产品-mock.php,但它使用单一产品文件夹中的模板文件。如何更改内容-单产品-mock.php中的模板路径,以便在单产品模拟文件夹中使用自定义文件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-11 22:28:24

可能有不止一种方法可以做到这一点,但它们都会改变在模板被包含之前过滤它的想法。

您可以完全跳过WooCommerce的simple-product.php模板(而不需要覆盖该模板),直接转到simple-product-mock.php并在那里创建所有内容。您可以通过过滤template_include来做到这一点。

代码语言:javascript
复制
add_filter( 'template_include', 'so_25789472_template_include' );

function so_25789472_template_include( $template ) {
  if ( is_singular('product') && (has_term( 'mock', 'product_cat')) ) {
    $template = get_stylesheet_directory() . '/woocommerce/single-product-mock.php';
  } 
  return $template;
}

您可以编辑single-product-mock.php以调用content-single-product-mock.php并对该文件进行硬编码。没有什么需要你继续使用Woo的钩子和函数。它们的目的只是为了让您更容易定制。

或者更棘手的是,您可以将模板(如single-product/title.php )复制到single-product-mock文件夹中.例:single-product-mock/title.php,然后每当我们在模拟类别中的单个产品模板上时,我们都会拦截对single-product/something.php模板的调用,如果它存在,将它们重定向到single-product-mock/something.php,如果不存在,则继续指向single-product/something.php。我们将通过woocommerce_locate_template过滤器来实现这一点。

代码语言:javascript
复制
add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );

function so_25789472_locate_template( $template, $template_name, $template_path ){

    // on single posts with mock category and only for single-product/something.php templates
    if( is_product() && has_term( 'mock', 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){

        // replace single-product with single-product-mock in template name
        $mock_template_name = str_replace("single-product/", "single-product-mock/", $template_name );

        // look for templates in the single-product-mock/ folder
        $mock_template = locate_template(
            array(
                trailingslashit( $template_path ) . $mock_template_name,
                $mock_template_name
            )
        );

        // if found, replace template with that in the single-product-mock/ folder
        if ( $mock_template ) {
            $template = $mock_template;
        }
    }

    return $template;
}

更新以过滤wc_get_template。

代码语言:javascript
复制
/**
 * Change wc template part for product with a specific category
 *
 * @param  string $templates
 * @param  string $slug
 * @param  string $name
 * @return string
 */
function so_25789472_get_template_part( $template, $slug, $name ) {
  if ( $slug == 'content' && $name = 'single-product' && has_term( 'test', 'product_cat' ) ) {
    $template = locate_template( array( WC()->template_path() . 'content-single-product-test.php' ) );
  } 
  return $template;
}
add_filter( 'wc_get_template_part', 'so_25789472_get_template_part', 10, 3 );
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25789472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档