获取当前产品类别中的产品

时间:2013-08-30 10:32:23

标签: php wordpress categories woocommerce product

我正在玩WOOCOMMERCE V2.0.13,我正在尝试显示当前产品类别中的每个产品(例如施工产品在施工页面上),我已设法显示当前类别中的单一产品,但如果产品也属于另一类别(例如建筑和环境),则当前类别会中断并显示来自建筑或环境的零产品。

如果我可以获得一些关于显示当前类别产品的建议/帮助,并允许它与多个类别的产品一起使用,那么我会非常感兴趣地提供帮助和时间。

我非常乐意重新编写整个部分以使其正常工作,以下是我的代码,如果我遗漏了任何内容,请告诉我。

谢谢。

<ul class="products"> 
    <?php
        global $post, $product;
        $categ = $product - > get_categories();
        $categ2 = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $categ); 
    ?> 
    <?php
        global $product;
        $args = array('post_type' = > 'product', 'posts_per_page' = > '999', 'product_cat' = > $categ2, );
        $loop = new WP_Query($args);
        while ($loop - > have_posts()): $loop - > the_post();
        global $product; 
    ?> 
        <li>
            <a href = "<?php echo get_permalink(); ?>"> 
                <?php
                    if (has_post_thumbnail()) {
                        $image = get_the_post_thumbnail($post - > ID, apply_filters('single_product_large_thumbnail_size', 'shop_single'));
                        $image_title = esc_attr(get_the_title(get_post_thumbnail_id()));
                        $image_link = get_permalink($product_id);
                        $attachment_count = count($product - > get_gallery_attachment_ids());
                        echo apply_filters('woocommerce_single_product_image_html', sprintf('<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s"  rel="prettyPhoto'.$gallery.'">%s</a>', $image_link, $image_title, $image), $post - > ID);
                    } else {
                        echo apply_filters('woocommerce_single_product_image_html', sprintf('<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src()), $post - > ID);
                } ?> 
            </a>
            <div>
                <h3>
                    <a href="<?php echo get_permalink(); ?>"><?php the_title();?></a> 
                    <span> 
                        <?php
                            if ($price_html = $product - > get_price_html()) { 
                        ?> 
                            <span class = "price">
                                <?php echo $price_html; ?>
                            </span> 
                        <?php } ?>
                    </span> 
                </h3>
            </div> 
            <div> 
                <p> 
                    <?php
                        $excerpt = get_the_excerpt();
                        echo string_limit_words($excerpt, 15); 
                    ?> 
                </p>
            </div> 
        </li>
    <?php endwhile; ?>
</ul>

Screenshot

1 个答案:

答案 0 :(得分:0)

 <?php if ( have_posts() ) : ?>
        <?php woocommerce_product_loop_start(); ?>
        <?php woocommerce_product_subcategories(); ?>
            <div class="courses-main">
                <ul class="products">
                    <?php while ( have_posts() ) : the_post(); ?>
                        <li>   
                            <a href="<?php echo get_permalink(); ?>">
                                <?php
                                    if ( has_post_thumbnail() ) {
                                        $image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
                                        $image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
                                        $image_link = get_permalink( $product_id );
                                        $attachment_count   = count( $product->get_gallery_attachment_ids() );
                                        echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s"  rel="prettyPhoto' . $gallery . '">%s</a>', $image_link, $image_title, $image ), $post->ID );
                                    } else {
                                        echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
                                    }
                                ?>
                            </a>
                            <div>
                                <h3>
                                    <a href="<?php echo get_permalink(); ?>"><?php the_title();?></a>
                                        <span>
                                            <?php if ( $price_html = $product->get_price_html()) { ?>
                                                <span class="price"><?php echo $price_html; ?></span> 
                                            <?php } ?>
                                        </span>
                                 </h3>
                            </div>
                            <div>
                                <p>
                                    <?php
                                        $excerpt = get_the_excerpt();
                                        echo string_limit_words($excerpt,15);
                                    ?>
                                </p>
                            </div>
                        </li>
                <?php endwhile;?>
            </ul>
        </div>
        <?php woocommerce_product_loop_end(); ?>
    <?php endif; ?>
相关问题