【问题标题】:woocommerce on page templates页面模板上的 woocommerce
【发布时间】:2016-05-16 23:21:56
【问题描述】:

我已经制作了一个新的页面模板并将 woocommerce archive-product.php 复制到其中,但是当商店页面有效时,自定义页面没有,有没有办法让它与商店页面功能相同?注意到它也不会从视觉作曲家或正常内容中提取任何其他内容。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    对于那些想要自定义 woocommerce 商店模板 (archive-product) 的人,这里有一个示例模板,可以任意程度地自定义。

    <?php
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    
    get_header( 'shop' ); ?>
    
    <?php
    /**
     * woocommerce_before_main_content hook
    *
    * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
    * @hooked woocommerce_breadcrumb - 20
    */
    do_action( 'woocommerce_before_main_content' ); ?>
    
    <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
    
        <h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
    
    <?php endif; ?>
    
    <?php do_action( 'woocommerce_archive_description' ); ?>
    
    <?php
    
    global $post, $product;
    
    $args = array( 
        'post_type'=>'product', 
        'posts_per_page'=>-1, 
        'orderby'=>'date', 
        'order'=>'ASC'      
    );
    // get all the posts ( here it would be all the wc products )
    $posts = get_posts( $args );
    
    /**
     * woocommerce_before_shop_loop hook
     *
     * @hooked woocommerce_result_count - 20
     * @hooked woocommerce_catalog_ordering - 30
     */
    do_action( 'woocommerce_before_shop_loop' );
    
    if( count( $posts ) > 0 ) { 
    
        woocommerce_product_loop_start();
    
        woocommerce_product_subcategories();
    
        foreach( $posts as $post ) {
            // this will put the current post into the GLOBAL $post object
            setup_postdata( $post );
            // this will put the product data into GLOBAL $product object
            wc_setup_product_data( $post ); ?>
    
            <!-- Now you have valid WP loop, put the content-product template here -->
            <?php wc_get_template_part( 'content', 'product' ); ?>
    
            <?php 
        }
    
        woocommerce_product_loop_end(); 
    
    } else {
        wc_get_template( 'loop/no-products-found.php' );
    }
    
    /**
     * woocommerce_after_shop_loop hook
     *
     * @hooked woocommerce_pagination - 10
     */
    do_action( 'woocommerce_after_shop_loop' );
    
    /**
     * woocommerce_sidebar hook
     *
     * @hooked woocommerce_get_sidebar - 10
     */
    do_action( 'woocommerce_sidebar' );
    
    get_footer( 'shop' ); ?>
    

    【讨论】:

    • 感谢这真的很有用。但只是一个问题,产品加载正常,但任何页面内容都没有加载,例如,我在该页面上的可视化作曲家中有一些不加载的组件。我需要一个 wp 循环来使它们出现吗?
    • 是的,这是可能的,把the_content();放在get_footer('shop');之前
    猜你喜欢
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 2017-02-03
    • 2012-10-08
    • 2016-11-04
    • 2018-11-30
    相关资源
    最近更新 更多