查看产品类别和已发布类别中的相关产品

时间:2018-10-07 08:38:26

标签: wordpress

很抱歉键入内容。 我已经在“帖子类别”部分中将WordPress中的单词分类了 我在产品类别中做了完全一样的事情 帖子中没有可自动显示相关产品类别的插件或代码?

1 个答案:

答案 0 :(得分:0)

  

WooCommerce-显示当前产品类别中的产品(当   查看单个产品)

<?php
if ( is_singular('product') ) {
  global $post;
  // get categories
  $terms = wp_get_post_terms( $post->ID, 'product_cat' );
  foreach ( $terms as $term ) $cats_array[] = $term->term_id;
  $query_args = array( 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array( 
    array(
      'taxonomy' => 'product_cat',
      'field' => 'id',
      'terms' => $cats_array
    )));
  $r = new WP_Query($query_args);

  if ($r->have_posts()) {
    ?>
    <ul class="product_list_widget">
      <?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
        <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
        <?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
        <?php if ( get_the_title() ) the_title(); else the_ID(); ?>
        </a> <?php echo $product->get_price_html(); ?></li>
      <?php endwhile; ?>
    </ul>
    <?php
    // Reset the global $the_post as this query will have stomped on it
    wp_reset_query();
  }
}
?>