如何根据类别ID显示woocommerce产品详细信息?

时间:2014-04-04 05:42:29

标签: wordpress woocommerce

有谁可以帮助我如何根据类别ID显示woocommerce产品详细信息?

我知道如何根据类别名称显示产品详细信息。代码是,

<ul class="productshome">
    <?php
        $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 2, 'product_cat' => 'Salwar-Kameez', 'orderby' =>'rand','order' => 'DESC' );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

                <li class="producthome">    

                    <a>post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">

                        <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>

                        <h3><?php the_title(); ?></h3>

                           <span class="price"><?php echo $product->get_price_html(); ?></span>

                    </a>

                    <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>

    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

</div>

在上面的代码'product_cat' => 'Salwar-Kameez'中将显示salwar-kameez的类别名称。但我需要根据category id

显示产品详情

1 个答案:

答案 0 :(得分:0)

'product_cat' => 'Salwar-Kameez'更改为tag_ID' => 15,其中15是类别ID。

此外,您在上面证明的代码也有一些错误。你没有关闭<ul>,你没有关闭<li>,最后有一个</div>但是没有打开div而你的链接不起作用。我修好了那些:

<ul class="productshome">
<?php
    $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 55, 'tag_ID' => 15, 'orderby' =>'rand','order' => 'DESC' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

            <li class="producthome">    

                <a href="<?php echo get_permalink($loop->post->ID) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">

                    <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>

                    <h3><?php the_title(); ?></h3>

                       <span class="price"><?php echo $product->get_price_html(); ?></span>

                </a>

                <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
             </li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

</ul>