gpt4 book ai didi

php - Woocommerce 使用 single-product.php 重定向的多个单一产品模板

转载 作者:行者123 更新时间:2023-12-03 20:32:23 25 4
gpt4 key购买 nike

我整天都在用这个拉头发,请原谅简短的描述,我只需要验证我的理智!

正如标题所说,我正在尝试在 woocommerce 中创建两个或三个不同的单产品布局。最低要求是拥有多个单一产品文件夹,每个文件夹都有自己的名称和配置。

无论我尝试以哪种方式覆盖 single-product.php 并使该文件使用逻辑来检查 product_cat 并相应地给出模板,我要么页面未加载,要么我写的内容被跳过并加载默认值。

到目前为止,我已经多次使用以下方法,试图将可能过时的代码拼凑在一起,否则会引起大惊小怪:

WooCommerce - How to create multiple single product template based on category?

Woocommerce single product - template by categories

Creating a different template file for certain Product Categories - Wordpress/Woocommerce?

我更希望有人可能知道一些我显然错过的事情,因为那里有很多关于尝试什么的文章,并且大多数人声称成功,但我无法这样做。

[更新] 使用 template_include来自@helgatheviking 的代码

目前还没有成功,但这就是我要做的事情;

文件结构
team-shops 是我想要获得的类别

  • /mytheme/woocommerce/single-product.php - 没有变化
  • /mytheme/woocommerce/content-single-product.php
  • /mytheme/woocommerce/single-product-team-shops.php - 将第 37 行更改为 <?php wc_get_template_part( 'content', 'single-product-team-shops' ); ?>
  • /mytheme/woocommerce/content-single-product-team-shops.php - 为#product-id(第 39 行)添加了额外的 ID
  • /mytheme/woocommerce/single-product-team-shops/文件夹,其中包含要更改的所有单个产品文件。

  • 正如我上面所说,这不起作用,但希望我提供的问题可能会更明显。

    再次感谢任何帮助 :)

    [我想我明白了]

    好的,所以我认为我有一些可行的方法,至少目前看来,还有一些进一步的测试要做,但任何想法都非常受欢迎,这就是我迄今为止与单一产品团队一起得到的东西-我的主题中的商店文件夹
    add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );

    function so_25789472_locate_template( $template, $template_name, $template_path ){

    $term_id = 2854;
    $taxonomy_name = 'product_cat';
    $term_children = get_term_children( $term_id, $taxonomy_name );

    foreach ( $term_children as $child ) {

    // on single posts with mock category and only for single-product/something.php templates
    if( is_product() && has_term( $child, 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){

    // replace single-product with single-product-mock in template name
    $mock_template_name = str_replace("single-product/", "single-product-team-shops/", $template_name );

    // look for templates in the single-product-mock/ folder
    $mock_template = locate_template(
    array(
    trailingslashit( $template_path ) . $mock_template_name,
    $mock_template_name
    )
    );

    // if found, replace template with that in the single-product-mock/ folder
    if ( $mock_template ) {
    $template = $mock_template;
    }
    }}

    return $template;
    }

    最佳答案

    使用 single-product-custom.php “自定义”类别中任何产品的模板:

    add_filter( 'template_include', 'so_43621049_template_include' );

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

    注意:如果您在 single-product-custom.php 中使用相同的 Action Hook 模板您将获得与默认 single-product.php 相同的外观.您可以“重命名”所有 Hook ,然后可以将现有功能(例如添加到购物车按钮的功能等)添加到新 Hook 中,以实现完全自定义的外观。

    关于php - Woocommerce 使用 single-product.php 重定向的多个单一产品模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43621049/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com