【问题标题】:Use a custom product-image.php file in woocommerce product page在 woocommerce 产品页面中使用自定义 product-image.php 文件
【发布时间】:2018-11-08 13:44:44
【问题描述】:

我是 Woocommerce 的新手。我想为书籍类别自定义 Woocommerce 产品页面的模板。我用自定义文件替换了 content-single-product.php 文件,修改了 single-product.php 文件中的代码,如下所示:

 <?php while ( have_posts() ) : the_post(); ?>
    <?php 
                global $post;
                $terms = wp_get_post_terms( $post->ID, 'product_cat' );
                foreach ( $terms as $term ) $categories[] = $term->slug;
                if ( in_array( 'livros', $categories ) ) {
                    wc_get_template_part( 'content', 'single-product-books' );              
                } else {
                    wc_get_template_part( 'content', 'single-product' );                
                }
                ?>
    <?php endwhile; // end of the loop. ?>

现在我想用这个 product-image-books.php 替换 product-image.php 文件。像这样尝试但不起作用:

global $post;
            $terms = wp_get_post_terms( $post->ID, 'product_cat' );
            foreach ( $terms as $term ) $categories[] = $term->slug;
            if ( in_array( 'books', $categories ) ) {
                wc_get_template_part( 'content', 'single-product-books' );
                wc_get_template_part( 'single-product/product', 'image-books' );
            } else {
                wc_get_template_part( 'content', 'single-product' );
                wc_get_template_part( 'content', 'product-image' );
            }
            ?>

此文件位于模板文件夹中:woocommerce > single-product.有什么帮助吗?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    单品图片使用woocommerce_show_product_images()函数展示并通过woocommerce_before_single_product_summary钩子钩住。

    因此,不要直接调用模板,而是先从该钩子中删除操作,然后在同一个钩子上添加您的自定义函数。

    您要显示图像的文件(product-image-books.phpproduct-image.php)必须位于wordpress\wp-content\themes\your-theme\woocommerce\single-product\ 目录中。

    请在您主题的functions.php 文件或自定义插件文件中添加以下代码。我已经测试了代码,它在我的安装中运行良好。

    remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
    
    add_action( 'woocommerce_before_single_product_summary', 'qt563_woocommerce_show_product_images', 20 );
    
    function qt563_woocommerce_show_product_images() {
        global $post;
        $terms = wp_get_post_terms( $post->ID, 'product_cat' );
        foreach ( $terms as $term ) $categories[] = $term->slug;
        if ( in_array( 'livros', $categories ) ) {
            wc_get_template( 'single-product/product-image-books.php' );              
        } else {
            wc_get_template( 'single-product/product-image.php' );                
        }
    }
    

    @Emmanu,您能否验证并接受它作为答案?

    【讨论】:

    • 工作文件!谢谢@Sarath :)
    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    相关资源
    最近更新 更多