Woocommerce 更改产品样式并将其显示在页面上

Woocommerce change product style and display it on a page

提前为我的近似英语道歉。我想更改 woocommerce 上特定产品类别的产品页面样式(不要显示图片和其他一些重要的布局更改)。已经成功并且有效,问题是我想在一个页面上显示这些产品。我尝试使用 woocommerce 集成简码 [product_page id=$id],问题是我在 content-single-product.php(在 woocommerce 文件夹中)上创建的自定义样式和布局未使用此简码应用.我试图在 class-ws-shortcodes.php(woocommerce 文件夹)上复制我的脚本,但它没有用。我应该怎么做才能在特定页面上显示产品,这些产品页面的真实样式(我创建的自定义页面),而不是 woocommerce 短代码?

提前感谢您的回答

您不能在 wordpress 的文件中进行编码,即 page.php 来修改 woocommerce 页面。这是错误的方式。您只需将 WooCommerce 页面覆盖到您的主题文件夹中,然后修改您想要执行的任何页面。所有 templates/pages 都位于 woocommerce/templates 文件夹中。

在这里您可以找到文档。

 http://docs.woothemes.com/document/template-structure/

为单个产品视图编辑woocommerce/templates/content-single-product。php

有两种方法可以做到这一点,这取决于你是想编辑single-product.php还是content-single-product.php。两者都会让你得到相同的最终结果。

要执行前者,您可以使用默认的 WordPress 功能并通过 template_include

在给定条件下过滤模板
add_filter( 'template_include', 'so_29984159_custom_category_template' );
function so_29984159_custom_category_template( $template ){
    if ( is_single() && get_post_type() == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
        $template = locate_template( 'single-product-custom.php' );
    }
    return $template;
}

或者,如果您更愿意创建自定义 content-product.php 模板部分,您可以使用 WooCommerce 的 wc_get_template_part,其工作方式几乎相同。 从技术上讲,我认为您可以使用相同的条件逻辑,但我在这里对其进行了调整以指出 WooCommerce 在过滤器中提供的变量。

add_filter( 'wc_get_template_part', 'so_29984159_custom_content_template_part', 10, 3 );
function so_29984159_custom_content_template_part( $template, $slug, $name ){
    if ( $slug == 'content' && $name == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
        $template = locate_template( 'content-product-custom.php' );
    }
    return $template;
}

在任何一种情况下,single-product-custom.phpcontent-product-custom.php 都将位于您主题的根文件夹中。要将它们放在其他地方,只需更改 locate_template() 参数中的路径即可。