【问题标题】:get custom woocommerce product获得定制的woocommerce产品
【发布时间】:2018-05-29 16:40:45
【问题描述】:

我想从 woocommerce 的一个类别中获得 10 种产品

例如,为了获取帖子类别的最新帖子,我使用以下代码

<?php $posts = get_posts( 'category=17&numberposts=5' ); ?>
<?php if( $posts ) : ?>
    <ul>
        <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
            <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><i class="circle"></i><?php the_title(); ?></a></li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

我想要一个代码,像这样用于获取 woocommerce 产品

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    试试这个例子:

    $args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page'        => '12',
        'meta_query'            => array(
            array(
                'key'           => '_visibility',
                'value'         => array('catalog', 'visible'),
                'compare'       => 'IN'
            )
        ),
        'tax_query'             => array(
            array(
                'taxonomy'      => 'product_cat',
                'field'         => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms'         => 26,
                'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
            )
        )
    );
    $products = new WP_Query($args);
    
    /* your loop */
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      试试这个例子,

      <?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();
      ?>
      

      <?php
      $args     = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
      $products = get_posts( $args ); 
      ?>
      

      希望对你有帮助。

      欲了解更多详情,请访问, Woocommerce get products

      【讨论】:

      • 谢谢你,它的工作:) 但这段代码不显示产品价格和“添加到购物车”链接,如何显示这个项目?
      • 如果这有效,请应用正确答案。所以这会帮助其他人。
      • 如果您以自定义方式显示产品,则需要添加代码显示您需要的东西。
      【解决方案3】:

      为什么不使用 woocommerce 的 product_category 短代码?

      <?php echo do_shortcode("[product_category category='17' limit='5']"); ?>
      

      它将列出与商店页面相同的产品。有了这些,您就可以安全地使用产品属性,例如缺货时。

      【讨论】:

        猜你喜欢
        • 2019-12-04
        • 1970-01-01
        • 2014-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 2021-05-16
        相关资源
        最近更新 更多