首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WooCommerce -如何根据产品类别创建单独的单一产品模板?

WooCommerce -如何根据产品类别创建单独的单一产品模板?
EN

Stack Overflow用户
提问于 2019-04-13 19:19:10
回答 1查看 287关注 0票数 0

我一直在阅读所有这些不同的问题和答案,比如here只是试图为应用产品类别实现一个单独的模板。

在我的例子中,我想使用产品类别“花式”(而不是“mock”)来加载一个单独的模板文件。也许我没有完全理解这些答案,但我目前在我的孩子主题的function.php中有以下内容:

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

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

而且它根本就不起作用。我的子主题/woocommerce/文件夹中有single-product-fancy.php和content-single-product-fancy.php。

single-product-fancy.php在第37行有以下内容:

代码语言:javascript
运行
复制
wc_get_template_part( 'content', 'single-product-fancy' );

content-single-product-fancy.php有我单独的模板布局

我有其他的变种,似乎可以工作,但随后会加载一个空白的白色页面的主页,我想是因为php的错误,不完全确定。

有人能帮我找出我做得不对的地方吗?

EN

回答 1

Stack Overflow用户

发布于 2019-04-13 19:42:01

我用这个解决了这个问题:

代码语言:javascript
运行
复制
add_filter( 'wc_get_template_part', 'so_25789472_get_template_part', 10, 3 );

/**
 * 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( 'fancy', 'product_cat' ) ) {
    $template = locate_template( array( WC()->template_path() . 'content-single-product-fancy.php' ) );
  } 
  return $template;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55664883

复制
相关文章

相似问题

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