Woocommerce 获取产品 [英] Woocommerce get products

查看:26
本文介绍了Woocommerce 获取产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码在我的 WordPress 网站中从 WooCommerce 获取产品类别列表:

I used the following code to get the list of product categories form WooCommerce in my WordPress website:

 <?php
  $taxonomy     = 'product_cat';
  $orderby      = 'name';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 0;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 0;
$args = array(
  'taxonomy'     => $taxonomy,
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title,
  'hide_empty'   => $empty
);
?>
<?php $all_categories = get_categories( $args );

//print_r($all_categories);
foreach ($all_categories as $cat) {
    //print_r($cat);
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;

?>     

<?php       

        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; ?>


        <?php
        $args2 = array(
          'taxonomy'     => $taxonomy,
          'child_of'     => 0,
          'parent'       => $category_id,
          'orderby'      => $orderby,
          'show_count'   => $show_count,
          'pad_counts'   => $pad_counts,
          'hierarchical' => $hierarchical,
          'title_li'     => $title,
          'hide_empty'   => $empty
        );
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo  $sub_category->name ;
            }

        } ?>



    <?php }     
}
?>

这工作正常并返回产品类别列表.我现在一直在尝试获取特定类别的产品列表.

This works fine and returns the list of product categories. I have been trying now to get a list of products for a particular category.

示例:使用 cat_id=34 获取所有产品.

Example: get all the products for with cat_id=34.

我知道产品被存储为帖子,并且一直在尝试完成这项工作,但似乎无法做到.

I know products are stored as posts, and have been trying to get this done but can't seem to.

如何获取特定类别的产品列表?

How do I get the list of products for a particular category?

推荐答案

<?php  
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 10,
        'product_cat'    => 'hoodies'
    );

    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();
        global $product;
        echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
    endwhile;

    wp_reset_query();
?>

这将列出所有产品缩略图和名称以及它们到产品页面的链接.根据您的要求更改类别名称和posts_per_page.

This will list all product thumbnails and names along with their links to product page. change the category name and posts_per_page as per your requirement.

这篇关于Woocommerce 获取产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆