单个产品页面的自定义布局 - Woocommerce [英] Custom Layout for Single Product page - Woocommerce

查看:85
本文介绍了单个产品页面的自定义布局 - Woocommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Woocommerce 中的一个产品页面创建自定义布局.我搜索并遵循了很多教程,但没有一个对我有用.我复制了single-product.php"并将其放入文件夹woocommerce"中的活动子主题中.然后我复制了它并将其重命名为single-product-landing.php".

I am trying to create a custom layout for one of our product pages in Woocommerce. I have searched and followed so many tutorials, but none are working for me. I copied 'single-product.php' and placed it into my active child theme in the folder 'woocommerce'. I then duplicated this and renamed it 'single-product-landing.php'.

然后我将以下代码放入我的functions.php页面:

I then placed the following code into my functions.php page:

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

function custom_single_product_template_include( $template ) {
if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-landing.php';
 } 
return $template;
}

然后我尝试更改 single-product-landing.php 中的项目,但没有任何更改,因此无法获取.为了确认,我已将该产品分配到自定义"类别.

Then I tried changing items in single-product-landing.php but nothing is changing, so it can't be picking it up. To confirm, I have the product assigned to the 'custom' category.

我还应该补充一点,我的产品是一个可变"产品,我不确定这是否对这里的任何内容有任何影响.

I should also add that my product is a 'variable' product, am not sure if that has any affect on anything here.

我在我的single.php"文件中发现了以下代码 - 我认为这可能会造成干扰.问题是,如果我删除它,我的任何页面上都不会显示任何内容(即使是那些不受新模板影响的页面):

I have found the following code in my 'single.php' file - I think this may be interfering. Problem is, if I delete this nothing shows on any of my pages (even those not affected by the new template):

 <?php
      if(get_theme_mod('product_layout') == 'custom') {
        wc_get_template_part( 'content', 'single-product-custom' );
      } else {
        wc_get_template_part( 'content', 'single-product' );
      }
  ?>

知道如何修改它以仍然显示内容但不否决我正在尝试进行的模板更改吗?

Any idea how I can modify this to still have content show but not over-rule the template change that i'm trying to make?

推荐答案

  1. 页面模板在 template_filter 运行后加载,因此将该代码添加到页面模板文件将不起作用.要运行该代码,请将相同的代码放在子主题的 functions.php 中.

  1. Page templates are loaded after the template_filter has been run, so adding that code to a page template file will have no effect. To make that run, place the same code in your child theme's functions.php.

add_filter 行末尾的 566 指的是过滤器的加载优先级,而不是类别 ID.简单地说,数字越大,过滤器加载的越晚.

The 566 at the end of the add_filter line refers to the filter's loading priority and not a category id. Simply put, the higher the number the later the filter will be loaded.

最后,可以使用 Woocommerce 函数稍微简化您的 if 语句 - if ( is_product() && ( has_term( 'custom', 'product_cat')) ) { - 没有太大区别,但更整洁.

Finally, your if statement can be simplified a little using Woocommerce functions - if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) { - doesn't make much difference, but it's tidier.

这篇关于单个产品页面的自定义布局 - Woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆