我想根据产品类别有多个不同的模板。正在查看Template for different category on woocommerce product 我有点成功了
我复制了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
SO网友:tiago calado
尝试改用get\\u template\\u part(“yourfile”)。
get\\u template\\u 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冲突。
另外,如果要从同一个文件调用单个产品模板,最好创建第二个文件并将代码粘贴到那里。