Woocommerce 3 中的自定义模板 [英] Custom templates in Woocommerce 3
问题描述
我正在尝试为一个 ID 为 5555 的产品制作一个单独的模板.从其页面中删除一张照片并更改块结构.覆盖此文件会影响所有产品页面.
I'm trying to make a separate template for only one product with ID 5555. To delete a photo from its page and change the blocks structure. Overriding this file affects all product pages.
wp-content\plugins\woocommerce\templates\content-single-product.php
逻辑上的解决方案是添加一个条件:如果产品单个prpduct页面的ID为5555,则为其页面使用另一个模板.我试过这个选项,但它不起作用.
The logical solution is to add a condition: if the product single prpduct page have ID 5555, then another template is used for its page. I tried this option, but it does not work.
/**
* Custom single product template.
*/
add_filter( 'single_template', 'get_custom_post_type_template' );
function get_custom_post_type_template($single_template) {
global $post;
if ($post->post_type == 'product') {
$single_template = dirname( __FILE__ ) . '/single-template.php';
}
return $single_template;
}
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
if ( is_page( 'slug' ) ) {
$new_template = locate_template( array( 'single-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
或者选项 2:移除这个钩子:
Or option 2: remove this hooks:
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
仅来自一个特定的产品页面:
from only one specific product page:
推荐答案
特别是自 Woocommerce 3.3 以来的自定义模板
由于 Woocommerce 3.3 Wordpress 钩子
template_include
似乎并不总是有效,正如您在 StackOverFlow 和其他地方最近的许多相关线程中看到的那样.
Custom templates specifically since Woocommerce 3.3
Since Woocommerce 3.3 Wordpress hook
template_include
doesn't seem to work always, as you could see in a lot of recent related threads in StackOverFlow and somewhere else too.
与 Woocommerce 模板相关的特定过滤器挂钩:
wc_get_template
wc_get_template_part
woocommerce_locate_template
woocommerce_template_path
(主题的覆盖模板文件夹)
案例 1. 使用 wc_get_template_part
(你的案例):
如果您查看 single-product.php
模板,content-single-product.php
模板在这一行加载了以下内容:
Case 1. Using wc_get_template_part
(your case):
If you look at single-product.php
template, the content-single-product.php
template is loaded with the following at this line:
<?php wc_get_template_part( 'content', 'single-product' ); ?>
所以我们将使用钩子 wc_get_template_part
.
So we will use the hook wc_get_template_part
.
假设您的自定义模板替换文件名为 content-single-product-custom.php
并位于 woocommerce
文件夹中您的活动子主题(或活动主题).要将此自定义模板用于特定产品 ID(5555
),您需要以下内容:
Let say that your custom template replacement file is named content-single-product-custom.php
and located in the woocommerce
folder of your active child theme (or active theme). To use this custom template for a specific product ID (5555
) you will need the following:
add_filter( 'wc_get_template_part', 'custom_wc_template_part', 10, 3 );
function custom_wc_template_part( $template, $slug, $name ) {
// The specific product ID
$product_id = 5555;
// The custom template file name
$custom_template_name = 'content-single-product-custom.php';
// For a specific product ID and content-single-product.php template
if( get_the_ID() == $product_id && $slug == 'content' && $name == 'single-product' ){
$template = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;
}
return $template;
}
此代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
This code goes in function.php file of your active child theme (or active theme). Tested and works.
如果模板是通过 wc_get_template 加载的()
函数,例如 single-product/meta.php
并且您的自定义替换模板名为 single-product/custom-meta.php代码> (位于活动子主题的 woocommerce 文件夹中).要将此自定义模板用于特定产品 ID(
5555
),您需要以下内容:
If templates is loaded via wc_get_template()
function, like for example single-product/meta.php
and your custom replacement template is named single-product/custom-meta.php
(and located in the woocommerce folder of your active child theme). To use this custom template for a specific product ID (5555
) you will need the following:
add_filter( 'wc_get_template', 'custom_wc_template', 10, 5 );
function custom_wc_template( $located, $template_name, $args, $template_path, $default_path ) {
// The specific product ID
$product_id = 5555;
// The custom template file name and path
$custom_template_name = 'single-product/custom-meta.php';
if( is_product() && get_the_ID() == 37 && $template_name == 'single-product/meta.php'){
$located = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;
}
return $located;
}
此代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
This code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于Woocommerce 3 中的自定义模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!