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

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

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

< p>产品的产品类别为“自定义”。</ p>

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

函数so_43621049_template_include($ template) {
if(is_singular('product')&amp;&amp;(has_term('custom','product_cat'))){
$ template = get_stylesheet_directory()。 '/woocommerce/single-product-mock.php';
}
返回
</ code> </ pre>
</ div>

展开原文

原文

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个回答



您的代码 正在使用作品。 你的代码也有点不完整:</ p>

  add_filter('template_include','custom_single_product_template_include',50,1); 

initual custom_single_product_template_include($ template){
if(is_singular) ('product')&amp;&amp;(has_term('custom','product_cat'))){
$ template = get_stylesheet_directory()。 '/woocommerce/single-product-mock.php';
}
返回$ template;
}
</ code> </ pre>

所以问题可能是 与</ strong> </ p>


  1. 该自定义模板的位置应该位于您的活动子主题</ strong中的'woocommerce'文件夹中 > (或在您的活动主题中)</ em>。</ li>
  2. 需要为您的主题启用Woocommerce支持。</ li>
    </ ol>

    < p>解决方案:</ p>


    1. 确保在您的活动子主题(或活动主题)</ em>中有一个“woocommerce”文件夹并添加到内部 它是您的自定义模板 single-product-mock.php </ code>

      (但不在“模板”</ strong>子文件夹中)</ em> < / li>
    2. 要检查是否启用了Woocommerce支持:


      • 复制“woocommerce”文件夹中的默认 single-product.php </ code>
        template 位于您的活动子主题(或活动主题)</ em> </ li>
      • 进入后端Woocommerce&gt; 状态...您通常会在“模板”部分(在本页末尾)看到:


        有一个活动的子主题:

        </ a>
        有一个活跃的主题:

        </ li>
      • 如果未启用</ strong>,则应搜索并 请查看相关的Woocommerce文档。</ li>
        </ ul> </ li>
        </ ol>

        其中一个应解决您的问题。</ p>
        < / DIV>

展开原文

原文

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.

duandaotui5633
duandaotui5633 这有效! 太棒了,非常感谢你。 @LoicTheAztec
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐