2018-03-11 16:03
浏览 265


I'm trying to use a custom page for only one of the products in the woocommerce shop. I've been trying to apply the attached function but without success (which code is coming from this answer).

I have a created a copy of the single-product.php file in my /woocommerce folder with some added code, but the single product view only shows the "standard" single-product-php and not my single-product-mock.php file.

And the product does have the product category "custom".

add_filter( 'template_include', 'so_43621049_template_include', 10 );

function so_43621049_template_include( $template ) {
  if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
    $template = get_stylesheet_directory() . '/woocommerce/single-product-mock.php';

图片转代码服务由CSDN问答提供 功能建议

我正在尝试仅为woocommerce商店中的一个产品使用自定义页面。 我一直在尝试应用附加功能但没有成功(代码来自 这个答案 )。

我创建了一个单品的副本 我的/ woocommerce文件夹中的.php文件有一些添加的代码,但单个产品视图只显示“标准”单产品-php而不是我的single-product-mock.php文件。 < p>产品的产品类别为“自定义”。

函数so_43621049_template_include($ template)  {
 $ template = get_stylesheet_directory()。  '/woocommerce/single-product-mock.php'; 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtol41388 2018-03-11 18:30

    The code that you are using works just perfectly. Your code is a bit incomplete too:

    add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
    function custom_single_product_template_include( $template ) {
        if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
            $template = get_stylesheet_directory() . '/woocommerce/single-product-mock.php';
        return $template;

    So the problem could be related to:

    1. The location of that custom template, that should be in a 'woocommerce' folder inside your active child theme (or inside your active theme).
    2. Woocommerce support need to be enable for your theme.


    1. be sure that inside your active child theme (or active theme) there is a "woocommerce" folder and add inside it your custom template single-product-mock.php
      (but not inside a "templates" sub-folder)
    2. To check Woocommerce support is enabled:
      • Copy the default single-product.php template inside the "woocommerce" folder located in your active child theme (or active theme)
      • Go in backend Woocommerce > Status … and you will normally see in "templates" section (at the end of this page):

        With an active child-theme:
        enter image description here With an active theme:
        enter image description here
      • If is not enable, you should search and look to the related Woocommerce documentation.

    One of those, should solve your issue.

    解决 无用
    打赏 举报

相关推荐 更多相似问题