如何制作自定义woocommerce类别模板?

时间:2019-03-16 17:40:21

标签: php wordpress woocommerce

我的主要问题是我不知道哪个文件用作所有woocommerce类别的类别模板。我正在尝试制作自己的woocommerce模板(作为wordpress模板),但遇到了一些问题。

这是我的 woocommerce.php 文件(作为主商店页面)

<?php  get_header(); ?>

<?php
if ( !is_shop()) {
     woocommerce_content();
} else {
?>

<!--Main Navigation-->
<header>

    <!--Main layout-->
<main>
    <div id="show" class="container">

        <!--Section: Dynamic Content Wrapper-->
        <section>
            <div class="dynamic-content"></div>
        </section>
        <!--Section: Dynamic Content Wrapper-->

        <!--Section: Products-->
        <section class="text-center">

            <!--Navbar-->
            <nav class="navbar navbar-expand-lg navbar-dark woobiboo-cyan my-5">

                <!-- Navbar brand -->
                <span class="navbar-brand">Categories:</span>

                <!-- Collapse button -->
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#basicExampleNav" aria-controls="basicExampleNav"
                    aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <!-- Collapsible content -->
                <div class="collapse navbar-collapse" id="basicExampleNav">

                    <!-- Links -->
                    <ul class="navbar-nav mr-auto">
                        <li class="nav-item active">
                            <a class="nav-link" href="#">All
                                <span class="sr-only">(current)</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Cat 1</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Cat 2</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Cat 3</a>
                        </li>

                    </ul>
                    <!-- Links -->

                    <form class="form-inline">
                        <div class="md-form mt-3 mb-0">
                            <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
                        </div>
                        <button class="btn btn-outline-white btn-md mt-3 ml-3" type="submit"><i class="fas fa-search"></i></button>
                    </form>
                </div>
                <!-- Collapsible content -->

            </nav>
            <!--/.Navbar-->

            <!--Grid row-->

            <div class="row wow fadeIn">
            <?php
                // Define custom query parameters
                $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                $args = array(
                    'post_type' => 'product',
                    'posts_per_page' => 30,
                    'paged'          => $paged
                    );
                $counter = 1;
                $loop = new WP_Query( $args );

                if ( $loop->have_posts() ) {
                    while ( $loop->have_posts() ) : $loop->the_post();
            ?>
                <!--Grid column-->
                <div class="col-lg-4 col-md-12 mb-4">

                    <!-- Product Card naked -->
                    <div class="card-naked">

                        <!--Featured image-->
                        <div class="view overlay hm-white-slight rounded mb-3">
                            <?php                                
                            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );
                            $regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
                            $sale_price = get_post_meta( get_the_ID(), '_sale_price', true);
                            $terms = get_the_terms( $post->ID, 'product_cat' );
                            foreach ($terms as $term) {
                                $product_cat_name = $term->name;
                                $product_cat_id = $term->term_id;
                                break;
                            }
                            ?>
                            <img src="<?php  echo $image[0]; ?>" class="img-fluid" data-id="<?php echo $loop->post->ID; ?>">
                            <a href ="<?php echo get_permalink() ?>">
                                <div class="mask"></div>
                            </a>
                        </div>

                        <!--Content-->
                        <h6 class="mb-3">
                            <a href="<?php echo esc_url( get_term_link( $product_cat_id, 'product_cat' ) ); ?>">
                                <span class="badge orange mr-1"><?php echo $product_cat_name ?></span>
                            </a>
                        </h6>
                        <h5 class="mb-3">
                            <strong><?php the_title() ?></strong>
                        </h5>
                        <p>
                            <?php if($sale_price) {
                            ?>
                            <span class="mr-1">
                                <del><?php echo $regular_price ?> zł</del>
                            </span>
                            <?php
                            }
                            ?>
                            <span>
                        <?php
                        echo  ($sale_price) ? $sale_price : $regular_price;
                        echo " zł";
                        ?></span>
                        </p>
                        <a href="<?php echo get_permalink(wc_get_page_id( 'cart' ))  . "?add-to-cart=" .  get_the_ID() ; ?> " class="btn woobiboo-cyan btn-sm" data-toggle="tooltip" data-placement="left" title="Add to cart">
                            <i class="fas fa-shopping-cart"></i>
                        </a>
                        <a href="<?php echo get_permalink() ?>" class="btn woobiboo-cyan btn-sm">Details</a>

                    </div>
                    <!-- Product Card naked -->

                </div>
                <!--Grid column-->
                <?php
                if ($counter % 3 == 0) {
                ?>
                    </div>
                    <!--Grid row-->
                    <!--Grid dynamic row-->
                    <div class="row wow fadeIn">
                <?php
                }
                $counter++;
                endwhile;
                } else {
                    echo __( 'No products found' );
                }
                // Custom query loop pagination

                ?>
            </div>
            <!--Grid row-->

        </section>
        <!--Section: Products-->

    </div>
</main>
<!--Main layout-->

</header>
<!--Main Navigation-->

<?php
} // end else (if single-product)
get_footer();
?>

一切正常,但是当我要进入类别页面时,它是默认的woocommerce页面。我尝试编辑 taxonomy-product_cat.php ,但没有用(也许是因为我使用的是woocommerce.php文件而不是 archive-product.php ?)< / p>

当我添加类似内容时:

'product_cat' => 'category_slug_here'

它可以工作,并且仅显示“ category_slug_here”类别中的产品。但是我想自动创建它(当我进入类别时,它将显示该类别中的项目)。

希望您能理解我的问题。也许有一种选择,我将制作自​​定义php文件(从woocommerce.php复制所有内容)并添加此product_cat =>,但我认为制作每个类别分隔的文件并不傻。

0 个答案:

没有答案