donglu8334 2018-03-11 08:03
浏览 275
已采纳

在Woocommerce中为特定产品类别使用自定义单一产品模板

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';
  } 
  retur
  • 写回答

1条回答 默认 最新

  • dtol41388 2018-03-11 10: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.

    Solutions:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 怎么实现数组的循环累加,simulink
  • ¥15 51单片机最小开发板系统,想让寻迹小车在全检测到黑线(寻迹模块代码在第一块板子上)时蜂鸣器响(在第二块板子上)
  • ¥15 pbootcms多选调用成列表
  • ¥15 51单片机oled显示时钟
  • ¥15 小规模TSP问题的动态规划求解
  • ¥25 kubelet.service: Failed with result 'exit-code'.
  • ¥15 bitvise黑框内一键部署v2ray提示账户没有root怎么解决
  • ¥15 车型识别以及相似度匹配中细节特征提取以及图像模糊问题
  • ¥15 怎么用鸿蒙的ArkTs写出来啊
  • ¥30 websocket服务端多线程通信
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部