Woocommerce 多个单一产品模板

Woocommerce 多个单一产品模板,woocommerce,Woocommerce,要让WooCommerce加载不同的“单一产品”模板,请执行以下操作: if( has_term( 'termgoeshere', 'product_cat' ) ) { $file = 'single-product-newtempforterm.php'; } else { $file = 'single-product.php'; } global $woocommerce; load_te

要让WooCommerce加载不同的“单一产品”模板,请执行以下操作:

if( has_term( 'termgoeshere', 'product_cat' ) ) {
        $file = 'single-product-newtempforterm.php';
            } else {
                $file = 'single-product.php';
    }

    global $woocommerce;

    load_template( $woocommerce->template_url . $file );
我遇到一个错误:

警告:require_once(woocommerce/single product wide.php):无法打开流:第572行的/home/xxx/public_html/wp includes/template.php中没有此类文件或目录

致命错误:require_once():无法在第572行的/home/xxx/public_html/wp includes/template.php中打开所需的'woocommerce/single product wide.php'(include_path='。:/usr/local/php54/pear')


我不确定从哪里开始解决问题。

您可以使用
wc\u get\u template\u part()
而不是
$woocommerce->template\u url

请记住,您不需要使用
.php

您可以使用
wc\u get\u template\u part()
而不是
$woocommerce->template\u url

请记住,您不需要使用
.php

对“自定义”类别中的任何产品使用single-product-custom.php模板:

 add_filter( 'template_include', 'so_43621049_template_include' );

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

注意:如果您在single-product-custom.php模板中使用相同的操作挂钩,您将获得与默认single-product.php相同的外观。您可以“重命名”所有挂钩,然后向新挂钩中添加现有功能(如添加到购物车按钮等),以实现完全自定义的外观。

对“自定义”类别中的任何产品使用single-product-custom.php模板:

 add_filter( 'template_include', 'so_43621049_template_include' );

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

注意:如果您在single-product-custom.php模板中使用相同的操作挂钩,您将获得与默认single-product.php相同的外观。您可以“重命名”所有挂钩,然后将现有功能(例如用于添加到购物车按钮的功能等)添加到新挂钩中,以实现完全自定义的外观。

$woocommerce->template\u url
自v2.1使用
wc\u get\u template\u part()以来已弃用
取而代之。
$woocommerce->template\u url
已被弃用,因为v2.1使用了
wc\u get\u template\u part()。