我一整天都在拔头发,请原谅简短说明,我只需要验证我的理智!
正如 Headers 所说,我正在尝试在woocommerce中创建两到三种不同的单一产品布局 . 尝试实现的最小目标是拥有多个单产品文件夹,每个文件夹都有自己的名称和配置 .
无论哪种方式,我尝试覆盖single-product.php,并使该文件使用逻辑来检查product_cat并相应地提供模板,我要么页面没有加载,要么我写的内容被跳过并且默认加载 .
到目前为止,我已经多次通过以下方法,试图拼凑可能过时的代码或导致所有大惊小怪:
WooCommerce - How to create multiple single product template based on category?
Woocommerce single product - template by categories
Creating a different template file for certain Product Categories - Wordpress/Woocommerce?
我更希望有人可能对此有所了解,我显然很遗憾,因为有很多关于尝试什么的文章,大多数都声称成功,但我无法这样做 .
[更新]使用@helgatheviking的 template_include
代码
目前还没有成功,但这是我要去的地方;
文件结构团队商店是我想要获得的类别
-
/mytheme/woocommerce/single-product.php - 没有变化
-
/mytheme/woocommerce/content-single-product.php
-
/mytheme/woocommerce/single-product-team-shops.php - 将第37行更改为
<?php wc_get_template_part( 'content', 'single-product-team-shops' ); ?>
-
/mytheme/woocommerce/content-single-product-team-shops.php - 为#product-id添加了额外的ID(第39行)
-
/ mytheme / woocommerce / single-product-team-shops /文件夹,包含要更改的所有单个产品文件 .
正如我上面所说,这不起作用,但希望我提供的问题可能更明显 .
再次感谢任何帮助 :)
[想想我懂了]
好的,所以我觉得我有一些有用的东西,至少现在它似乎还有一些进一步的测试要做,但任何想法都不过是受欢迎的,这就是我到目前为止所拥有的单一产品团队我的主题中的-shops文件夹
add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );
function so_25789472_locate_template( $template, $template_name, $template_path ){
$term_id = 2854;
$taxonomy_name = 'product_cat';
$term_children = get_term_children( $term_id, $taxonomy_name );
foreach ( $term_children as $child ) {
// on single posts with mock category and only for single-product/something.php templates
if( is_product() && has_term( $child, '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-team-shops/", $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;
}
1 回答
对"custom"类别中的任何产品使用
single-product-custom.php
模板:注意:如果在
single-product-custom.php
模板中使用相同的动作挂钩,您将获得与默认single-product.php
相同的外观 . 您可以'rename'所有钩子然后可以添加现有功能(例如添加到购物车按钮等的那些)到新的钩子,以实现完全自定义的外观 .