制作自定义WooCommerce循环

时间:2012-11-07 09:49:24

标签: wordpress woocommerce

我正在使用已设计的wordpress主题,而现在我想展示WooCommerce产品(我自己定制的自定义帖子类型),而不是常规博客文章。

这是显示循环的当前查询:

<?php 
                $args = array(
                    //'posts_per_page' => '2',
                    'paged' => get_query_var('paged')
                );
                $homepage_query = new WP_Query($args);
            ?>
            <?php //query_posts('posts_per_page=4&paged='.get_query_var('paged')); ?>
            <?php if ( have_posts() ) : ?>  
                <?php while ( $homepage_query->have_posts() ) : $homepage_query->the_post(); ?>
                    <?php if($style == 'blog_style') { ?>
                    <div id="blog-style" class="post-box">
                        <?php get_template_part('content', 'blog'); ?>
                    </div>
                    <?php } else { ?>
                    <div class="post-box grid_4 <?php aero_post_box_class(); ?>">
                        <?php get_template_part('content', ''); ?>
                    </div>
                    <?php } ?>
                <?php endwhile; ?>

有没有办法向$args添加选项,以便循环显示WooCommerce产品?我也在使用这个循环的分页,这在这个项目中是必需的,所以这就是使用这个循环很重要的原因。

3 个答案:

答案 0 :(得分:24)

您应该能够通过循环访问产品,将post_type arg设置为product

<?php

// Setup your custom query
$args = array( 'post_type' => 'product', ... );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <a href="<?php echo get_permalink( $loop->post->ID ) ?>">
        <?php the_title(); ?>
    </a>

<?php endwhile; wp_reset_query(); // Remember to reset ?>

答案 1 :(得分:0)

这是重新创建和自定义WooCommerce产品循环的正确方法:

  if(!function_exists('wc_get_products')) {
    return;
  }
  $paged                   = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
  $ordering                = WC()->query->get_catalog_ordering_args();
  $ordering['orderby']     = array_shift(explode(' ', $ordering['orderby']));
  $ordering['orderby']     = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
  $products_per_page       = apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());

  $products_ids            = wc_get_products(array(
    'status'               => 'publish',
    'limit'                => $products_per_page,
    'page'                 => $paged,
    'paginate'             => true,
    'return'               => 'ids',
    'orderby'              => $ordering['orderby'],
    'order'                => $ordering['order'],
  ));

  wc_set_loop_prop('current_page', $paged);
  wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
  wc_set_loop_prop('page_template', get_page_template_slug());
  wc_set_loop_prop('per_page', $products_per_page);
  wc_set_loop_prop('total', $products_ids->total);
  wc_set_loop_prop('total_pages', $products_ids->max_num_pages);

  if($products_ids) {
    do_action('woocommerce_before_shop_loop');
    woocommerce_product_loop_start();
      foreach($products_ids->products as $featured_product) {
        $post_object = get_post($featured_product);
        setup_postdata($GLOBALS['post'] =& $post_object);
        wc_get_template_part('content', 'product');
      }
      wp_reset_postdata();
    woocommerce_product_loop_end();
    do_action('woocommerce_after_shop_loop');
  } else {
    do_action('woocommerce_no_products_found');
  }

使用上面的代码,您将自定义wc_get_products()参数以获取所需产品的ID(如果有特定条件的话)。一旦该代码到位,就可以使用WooCommerce本地循环的所有功能-分页,订购等。This method is superior to WP_Query and get_posts() because those two methods can break

我在此处撰写了有关自定义WooCommerce循环的更详细的博客文章:https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/

答案 2 :(得分:-1)

您还可以使用该代码获取类别

       $terms = get_terms('product_cat');

       foreach ($terms as $term) {
         $term_link =  get_term_link( $term, 'product_cat' );
         echo '<li><a href="' . $term_link . '">' . $term->name . '</a></li>';
       }

如果您只想要父类别

wp_list_categories('taxonomy=product_cat&orderby=order&title_li=&depth=1');