【问题标题】:Show random products via php excluding one category通过 php 显示随机产品,不包括一个类别
【发布时间】:2017-09-20 16:40:29
【问题描述】:

在 WooCommerce 中,我使用脚本来显示一些随机产品,但现在我需要排除一个我不需要出现在本节中的产品类别。

如何在我的代码中做到这一点?

我的代码:

<?php
    global $product; 
    $args = array(
        'posts_per_page'   => 4,
        'orderby'          => 'rand',
        'post_type'        => 'product' 
    );
    $random_products = get_posts( $args );

    foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
        <li class="single_product_lower_widget" style="list-style:none;">
        <a href="<?php the_permalink(); ?>">
            <span class="single_product_lower_widget_image">
            <?php the_post_thumbnail() ?>
            <span class="product-title"><?php the_title(); ?></span>
            </span>
            <p><?php get_post_meta( $post->ID, '_price', true ); ?></p>
        </a>
        </li>
    <?php 
    endforeach; 
    wp_reset_postdata();
?>

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    您需要以这种方式使用 'tax_query'(定义不需要的产品类别):

    <?php
        // Set HERE your product category (to be excluded)
        $category_slug = 'music' ;
        $random_products = get_posts( array(
            'posts_per_page'   => 4,
            'orderby'          => 'rand',
            'post_status'      => 'publish',
            'post_type'        => 'product',
            'tax_query' => array( array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => $category_slug,
                'operator' => 'NOT IN',
            ))
        ));
    
        foreach ( $random_products as $post ) : setup_postdata( $post );
    ?>
        <li class="single_product_lower_widget" style="list-style:none;">
            <a href="<?php the_permalink(); ?>">
                <span class="single_product_lower_widget_image">
                <?php the_post_thumbnail() ?>
                <span class="product-title"><?php the_title(); ?></span>
                </span>
                <p><?php get_post_meta( $post->ID, '_price', true ); ?></p>
            </a>
        </li>
    <?php
        endforeach;
        wp_reset_postdata();
    ?>
    

    经过测试并且可以工作

    【讨论】:

      【解决方案2】:
      <?php
      // Set HERE your product category (to be excluded)
      $category_slug = 'music' ;
      $random_products = get_posts( array(
          'posts_per_page'   => 4,
          'orderby'          => 'rand',
          'post_status'      => 'publish',
          'post_type'        => 'product',
          'tax_query' => array( array(
              'taxonomy' => 'product_cat',
              'field'    => 'slug',
              'terms'    => $category_slug,
              'operator' => 'NOT IN',
          ))
      ));
      
      foreach ( $random_products as $post ) : setup_postdata( $post );
       ?>
      <li class="single_product_lower_widget" style="list-style:none;">
          <a href="<?php the_permalink(); ?>">
              <span class="single_product_lower_widget_image">
              <?php the_post_thumbnail() ?>
              <span class="product-title"><?php the_title(); ?></span>
              </span>
              <p><?php get_post_meta( $post->ID, '_price', true ); ?></p>
          </a>
      </li>
      <?php
      endforeach;
      wp_reset_postdata();
       ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多