使用single-product.php重定向多个单产品模板
我一整天都在胡思乱想,请原谅我的简短描述,我只需要验证我的理智 正如标题所说,我正试图在woocommerce中创建两个或三个不同的单一产品布局。尝试实现的最低要求是拥有多个单个产品文件夹,每个文件夹都有自己的名称和配置 无论我尝试以何种方式重写single-product.php并使此文件使用逻辑检查product_cat并相应地给出模板,我都会跳过未加载的页面或我编写的内容,并加载默认值 到目前为止,我已经使用了以下方法多次,试图拼凑出可能过时的代码,或者以其他方式引起所有的麻烦: 我更希望有人知道一些关于这方面的事情,但我显然没有,因为有很多关于尝试什么的文章,大多数都声称成功,但我无法做到这一点 [更新]使用使用single-product.php重定向多个单产品模板,php,wordpress,woocommerce,logic,Php,Wordpress,Woocommerce,Logic,我一整天都在胡思乱想,请原谅我的简短描述,我只需要验证我的理智 正如标题所说,我正试图在woocommerce中创建两个或三个不同的单一产品布局。尝试实现的最低要求是拥有多个单个产品文件夹,每个文件夹都有自己的名称和配置 无论我尝试以何种方式重写single-product.php并使此文件使用逻辑检查product_cat并相应地给出模板,我都会跳过未加载的页面或我编写的内容,并加载默认值 到目前为止,我已经使用了以下方法多次,试图拼凑出可能过时的代码,或者以其他方式引起所有的麻烦: 我
模板\u包括@helgatheviking的代码
目前还没有成功,但这就是我的目标
文件结构
团队商店是我想要的类别
- /mytheme/woocommerce/single-product.php-无更改
- /mytheme/woocommerce/content-single-product.php
- /mytheme/woocommerce/single-product-team-shops.php-将第37行更改为
- /mytheme/woocommerce/content-single-product-team-shops.php-在#产品id(第39行)中添加了额外的id
- /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;
}
对“自定义”类别中的任何产品使用单一产品custom.php
模板:
add_filter( 'template_include', 'so_43621049_template_include' );
function so_43621049_template_include( $template ) {
if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-custom.php';
}
return $template;
}
注意:如果您在single-product custom.php
模板中使用相同的操作挂钩,您将获得与默认single-product.php
相同的外观。您可以“重命名”所有挂钩,然后将现有功能(例如用于添加到购物车按钮的功能等)添加到新挂钩,以实现完全自定义的外观 谢谢,我有大约50个类别,可以简化上面的多个定制产品页面,这适用于2
add_filter( 'template_include', 'bsl_single_product_template_include', 50, 1 );
function bsl_single_product_template_include( $template ) {
if ( is_singular('product') && (has_term( 'Battery Scissor Lifts', 'product_cat')) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-battery-scissor-lifts.php';
}
return $template;
}
add_filter( 'template_include', 'dfsl_single_product_template_include', 50, 1 );
function dfsl_single_product_template_include( $template ) {
if ( is_singular('product') && (has_term( 'Diesel Fuel Scissor Lifts', 'product_cat')) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-diesel-fuel-scissor-lifts.php';
}
return $template;
}
我想说这听起来非常熟悉,但在你的第一个链接中有我的答案。我的template\u include
方法应该可以让您运行多个单一产品模板。它看起来没有过时(虽然我现在无法重新测试),所以请分享您的代码版本,并解释您存储模板的位置。谢谢@helgatheviking,我将根据您的链接进行所有设置,并回复给您,谢谢联系!更新了@helgathevikinghy,@helgatheviking另一个更新,上面的代码确实有效,但是,只影响content-single-product.PHP,请原谅我的无知。但是,您建议的第二种方法非常有效,因此我目前已经启动并运行了它。我在使用这种方法时遇到的唯一问题是“has_term”不允许我使用父项,我一直尝试使用“get_term_children”但没有成功,我将很快更新该代码,再次感谢:)如果您在content single product custom.php
中使用相同的操作挂钩,您将获得与默认custom single product.php
相同的外观。这是一个很好的提示,而且似乎是最好、最清晰的方法。只是不明白你是怎么“重命名”钩子的。我知道我可以做add_动作('woocommerce_在单个产品之前,'mysteme_dostuff',10)代码>但这适用于所有模板。重命名是如何实现的/从哪里实现的?在single product custom.php
中,您可以将woocommerce\u-before\u-single\u-product
操作挂钩切换到do\u action('my\u-theme\u-before\u-single\u-product')
,然后您可以按任何顺序向新挂钩添加操作。嘿,@helgatheviking我遇到了一个与此代码相关的错误,即woocommerce选项卡中的reviews选项卡完全丢失,但仅在原始的单一产品模板上,使用新创建的模板文件时,所有内容都显示良好Hi Thian,这不是答案。请把这当作一个问题来问。