我一直在用这一整天拉我的头发,请原谅简短的描述,我只需要验证我的理智!Woocommerce使用single-product.php重定向的多个单一产品模板
正如标题所说,我试图在woocommerce中创建两个或三个不同的单一产品布局。最低限度试图达到的目标是拥有多个单独的产品文件夹,每个文件夹都有自己的名称和配置。
无论我试图重写单一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?
我更希望有人可能知道这件事,我明显失踪了,因为那里有很多关于该怎么尝试和多数宣称成功的文章但我无法这样做。
[更新]使用template_include
代码@helgatheviking
没有成功,只是还没有,但这里的地方我到;
文件结构 团队商店是我试图让
- /mytheme/woocommerce/single-product.php类别 - 没有改变
- /mytheme的/ woocommerce /内容单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 - 加入另外的id来#product-id(line 39)
- /mytheme/woocommerce/single-product-team-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;
}
我想说这听起来非常熟悉,但在我的第一个链接[这里](http://stackoverflow.com/a/25797963/383847)中有答案。我的'template_include'方法应该让你运行多个单一产品模板。它没有看起来过时(尽管我现在无法重新测试),所以请分享您的代码版本并解释您存储模板的位置。 – helgatheviking
感谢@helgatheviking生病根据您的链接获取所有设置并回复您,感谢您的支持! –
已更新@helgatheviking –