2012-11-07 93 views
7

我正在使用WordPress已设计的主题,现在,而不是定期的博客文章,我想展示WooCommerce产品(这是我persume自定义职位类型)。制作自定义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产品?我也使用这个循环的分页,这是这个项目所需要的,所以这就是为什么使用这个循环很重要。

回答

22

您应该能够通过循环来访问产品,设置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 ?> 
+0

是的,它现在循环,太棒了! – jOpacic

+8

如果你想得到价格等,你可能想要做的:'$ product = get_product($ loop-> post);'然后像使用'WC_Product'一样使用它:'echo $ product-> get_price_html( );等等 – Ciantic

0

可以使用THI代码

 $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');