Але*_*ова 3 php wordpress templates product woocommerce
我正在尝试仅为ID 5555的一种产品制作单独的模板。要从其页面中删除照片并更改块结构。覆盖此文件会影响所有产品页面。
wp-content\plugins\woocommerce\templates\content-single-product.php
Run Code Online (Sandbox Code Playgroud)
逻辑解决方案是添加一个条件:如果产品单个产品页面的ID为5555,则将另一个模板用于其页面。我尝试了此选项,但是它不起作用。
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
或选项2:移除以下挂钩:
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
Run Code Online (Sandbox Code Playgroud)
仅在一个特定的产品页面上:
自Woocommerce 3.3以来的自定义模板
从Woocommerce 3.3开始,Wordpress钩子
template_include
似乎并不总是起作用,就像您在StackOverFlow以及许多其他地方的最近相关线程中所看到的那样。
与Woocommerce模板相关的特定过滤器挂钩:
wc_get_template_part
(您的情况):如果您查看single-product.php
模板,则content-single-product.php
此行在模板中加载了以下内容:
<?php wc_get_template_part( 'content', 'single-product' ); ?>
Run Code Online (Sandbox Code Playgroud)
所以我们将使用钩子wc_get_template_part
。
假设您的自定义模板替换文件已命名content-single-product-custom.php
并位于woocommerce
活动子主题(或活动主题)的文件夹中。要将此自定义模板用于特定的产品ID (5555
),您将需要以下内容:
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;
}
Run Code Online (Sandbox Code Playgroud)
这段代码在您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。
wc_get_template
:如果模板是通过wc_get_template()
函数加载的,例如,single-product/meta.php
并且您的自定义替换模板已命名single-product/custom-meta.php
(位于活动子主题的woocommerce文件夹中)。要将此自定义模板用于特定的产品ID (5555
),您将需要以下内容:
add_filter( 'wc_get_template', 'custom_wc_template', 10, 5 );
function custom_wc_templates( $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/meta-custom.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;
}
Run Code Online (Sandbox Code Playgroud)
这段代码在您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。
归档时间: |
|
查看次数: |
600 次 |
最近记录: |