我希望有多个不同的模板取决于产品类别。看着关于woocommerce产品不同类别的模板,我让它起作用了
我已将single-product.php复制到theme-folder/woocommerce/中,并将代码更新为
<?php the_post(); ?>
<?php
//wc_get_template_part( 'content', 'single-product' );
if( has_term( 'course', 'product_cat' ) ) {
wc_get_template_part( 'content', 'single-product-course' );
} else {
wc_get_template_part( 'content', 'single-product' );
}
?>
<?php endwhile; // end of the loop. ?>然后我在theme-folder/woocommerce/ - content-single-product.php和content-single-product-course.php中创建了两个文件
当查看单个产品时,两者似乎都显示得很好,但是,使用course查看产品模板会显示一个错误(尽管它仍然呈现该HTML)。
Warning: include(/site/wp-content/plugins/woocommerce-bookings/templates/booking-form/.php): failed to open stream: No such file or directory in /site/wp-content/plugins/woocommerce/includes/wc-core-functions.php on line 345
# Time Memory Function Location
1 0.0009 413744 {main}( ) .../index.php:0
2 0.0010 415328 require( '/site/wp-blog-header.php' ) .../index.php:17
3 1.8879 63328264 require_once( '/site/wp-includes/template-loader.php' ) .../wp-blog-header.php:19
4 1.9015 63307352 include( '/site/wp-content/themes/dyfi-storefront-child/woocommerce/single-product.php' ) .../template-loader.php:106
5 1.9950 64097248 wc_get_template_part( ) .../single-product.php:44
6 1.9952 64098072 load_template( ) .../wc-core-functions.php:284
7 1.9954 64101152 require( '/site/wp-content/themes/dyfi-storefront-child/woocommerce/content-single-product-course.php' ) .../template.php:772
8 1.9970 64113712 do_action( ) .../content-single-product-course.php:62
9 1.9970 64114088 WP_Hook->do_action( ) .../plugin.php:470
10 1.9970 64114088 WP_Hook->apply_filters( ) .../class-wp-hook.php:327
11 2.0539 64450192 woocommerce_template_single_add_to_cart( ) .../class-wp-hook.php:303
12 2.0539 64450248 do_action( ) .../wc-template-functions.php:1667
13 2.0540 64450624 WP_Hook->do_action( ) .../plugin.php:470
14 2.0540 64450624 WP_Hook->apply_filters( ) .../class-wp-hook.php:327
15 2.0540 64451752 WC_Booking_Cart_Manager->add_to_cart( ) .../class-wp-hook.php:303
16 2.0554 64565880 wc_get_template( ) .../class-wc-booking-cart-manager.php:63
17 2.0558 64573392 include( '/site/wp-content/plugins/woocommerce-bookings/templates/single-product/add-to-cart/booking.php' ) .../wc-core-functions.php:345
18 2.0559 64573432 WC_Booking_Form->output( ) .../booking.php:41
19 2.0993 64632408 wc_get_template( ) .../class-wc-booking-form.php:400发布于 2021-09-01 19:34:37
尝试使用get_template_part('yourfile')代替。
get_template_part()从主题根路径开始。
因此,如果在woocommerce文件夹中有模板,请使用:
if( has_term( 'course', 'product_cat' ) ) {
get_template_part('woocommerce/single-product-course' );
} else {
wc_get_template_part( 'content', 'single-product' );
}但是,最好是创建一个不是woocommerce的文件夹,一旦woocoomerce文件夹搜索woocommerce文件,如果某个地方有一个文件名与make冲突。
另外,您是否从同一个文件调用单一产品模板,可能最好是创建第二个文件并将代码粘贴到那里。
https://wordpress.stackexchange.com/questions/393999
复制相似问题