我一直在阅读所有这些不同的问题和答案,比如here只是试图为应用产品类别实现一个单独的模板。
在我的例子中,我想使用产品类别“花式”(而不是“mock”)来加载一个单独的模板文件。也许我没有完全理解这些答案,但我目前在我的孩子主题的function.php中有以下内容:
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行有以下内容:
wc_get_template_part( 'content', 'single-product-fancy' );content-single-product-fancy.php有我单独的模板布局
我有其他的变种,似乎可以工作,但随后会加载一个空白的白色页面的主页,我想是因为php的错误,不完全确定。
有人能帮我找出我做得不对的地方吗?
发布于 2019-04-13 19:42:01
我用这个解决了这个问题:
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;
}https://stackoverflow.com/questions/55664883
复制相似问题