Php WooCommerce-如何基于类别创建多个单一产品模板?
您好,我对woocommerce是相当陌生的,我的店铺有四类产品,它们使用相同的单一产品模板。我想添加第五类产品,其中产品页面布局与已经使用的页面布局非常不同 这是文件结构-Php WooCommerce-如何基于类别创建多个单一产品模板?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,您好,我对woocommerce是相当陌生的,我的店铺有四类产品,它们使用相同的单一产品模板。我想添加第五类产品,其中产品页面布局与已经使用的页面布局非常不同 这是文件结构- 主题/商业/单一产品/ 主题/商业/单一产品模拟/ theme/woocommerce/single-product mock/title.php theme/woocommerce/content-single-product-mock.php theme/woocommerce/single-product.php
- 主题/商业/单一产品/
- 主题/商业/单一产品模拟/
- theme/woocommerce/single-product mock/title.php
- theme/woocommerce/content-single-product-mock.php
- theme/woocommerce/single-product.php
<?php if (has_term( 'mock', 'product_cat' )) {
woocommerce_get_template_part( 'content', 'single-product-mock' );
} else{
wc_get_template_part( 'content', 'single-product' );
} ?>
对于类别mock,它重定向到content-single-product-mock.php,但它使用single-product文件夹中的模板文件。如何更改content-single-product-mock.php中的模板路径,以便使用single-product-mock文件夹中的自定义文件?实现这一点的方法可能不止一种,但它们都改变了在包含模板之前过滤模板的想法 您可以完全跳过WooCommerce的
simple product.php
模板(无需覆盖该模板),直接转到simple product mock.php
并在那里创建所有内容。你可以通过过滤来做到这一点
您可以编辑singleproductmock.php
来调用content singleproduct mock.php
并硬编码该文件。没有什么需要你继续使用吴的钩子和函数。它们的目的只是让您可以轻松地进行自定义
或者非常棘手的是,您可以将诸如single-product/title.php
之类的模板复制到single-product-mock
文件夹中。。。例如:single-product mock/title.php
,然后每当我们在mock类别中的单个产品模板上时,我们将拦截对single-product/something.php
模板的调用,并将它们重定向到single-product-mock/something.php
,如果它存在,则继续指向single-product/something.php
。我们将通过woocommerce\u locate\u模板
过滤器执行此操作
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\u get\u模板
/**
* 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 );
天啊!非常感谢你。最终使用了woocommerce\u locate\u模板文件管理器。它工作正常:)
/**
* 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 );