Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在WP自定义循环中显示随机特色产品(woocommerce)?_Php_Wordpress_Loops_Woocommerce_Featured - Fatal编程技术网

Php 如何在WP自定义循环中显示随机特色产品(woocommerce)?

Php 如何在WP自定义循环中显示随机特色产品(woocommerce)?,php,wordpress,loops,woocommerce,featured,Php,Wordpress,Loops,Woocommerce,Featured,我正在尝试在WoodPress中为Woocommerce产品创建一个自定义循环。我想在循环中显示一个随机的特色产品。但由于某些原因,它没有正确地理解我的论点,并从所有可用的产品中随机选取一个产品 这就是我目前正在使用的代码。它确实显示了一个随机的产品,但它忽略了代码的特征部分 $args = array( 'posts_per_page' => 1, 'orderby' => 'rand', 'post_type' =>

我正在尝试在WoodPress中为Woocommerce产品创建一个自定义循环。我想在循环中显示一个随机的特色产品。但由于某些原因,它没有正确地理解我的论点,并从所有可用的产品中随机选取一个产品

这就是我目前正在使用的代码。它确实显示了一个随机的产品,但它忽略了代码的特征部分

$args = array(
    'posts_per_page'   => 1,
    'orderby'          => 'rand',
    'post_type'        => 'product',
    'meta_query'  => array(
        'key'     => '_featured',
        'value'   => 'yes'
    )
);

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

<li>
    <a href="<?php echo the_permalink(); ?>">
        <h3><?php the_title(); ?></h3>
    </a>
</li>

<?php endwhile;
wp_reset_query(); ?>
$args=array(
“每页帖子数”=>1,
'orderby'=>'rand',
“post_类型”=>“产品”,
“元查询”=>数组(
“键”=>“\u特征”,
“值”=>“是”
)
);
$loop=新的WP_查询($args);
而($loop->have_posts()):$loop->the_post();全球$product;?>
  • 有人能把我引向正确的方向吗


    提前谢谢

    我认为您的键值数组在预期的数组层次结构中位置太高,请尝试以下方法:

    $args = array(
        'posts_per_page'   => 1,
        'orderby'          => 'rand',
        'post_type'        => 'product',
        'meta_query'  => array(
            array(
                'key'     => '_featured',
                'value'   => 'yes',
            )
        )
    );
    

    我认为您的键值数组在预期的数组层次结构中位置太高,请尝试以下方法:

    $args = array(
        'posts_per_page'   => 1,
        'orderby'          => 'rand',
        'post_type'        => 'product',
        'meta_query'  => array(
            array(
                'key'     => '_featured',
                'value'   => 'yes',
            )
        )
    );
    

    我刚刚遇到这个

    这不是直接针对你的问题,但可能是它的基础

    功能项目似乎不再存储为元:

        $meta_query  = WC()->query->get_meta_query();
        $tax_query   = WC()->query->get_tax_query();
        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured',
            'operator' => 'IN',
        );
    
    $query_args = array(
        'post_type'           => 'product',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page'      => 1,
        'meta_query'          => $meta_query,
        'tax_query'           => $tax_query,
    );`
    

    我刚刚遇到这个

    这不是直接针对你的问题,但可能是它的基础

    功能项目似乎不再存储为元:

        $meta_query  = WC()->query->get_meta_query();
        $tax_query   = WC()->query->get_tax_query();
        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured',
            'operator' => 'IN',
        );
    
    $query_args = array(
        'post_type'           => 'product',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page'      => 1,
        'meta_query'          => $meta_query,
        'tax_query'           => $tax_query,
    );`
    

    我也有同样的问题。试试这个!为我工作

    <?php
             $featured_query = new WP_Query( array(
                 'tax_query' => array(
                         array(
                         'taxonomy' => 'product_visibility',
                         'field'    => 'name',
                         'terms'    => 'featured',
                         'operator' => 'IN'
                     ),
              ),
         ) );
    ?>
    

    我也遇到了同样的问题。试试这个!为我工作

    <?php
             $featured_query = new WP_Query( array(
                 'tax_query' => array(
                         array(
                         'taxonomy' => 'product_visibility',
                         'field'    => 'name',
                         'terms'    => 'featured',
                         'operator' => 'IN'
                     ),
              ),
         ) );
    ?>
    

    WooCommerce 3中的特色产品循环

    <ul class="products">
    <?php
        $args = array(
            'post_type'      => 'product',
            'posts_per_page' => 12,
            'orderby'        => 'rand',
            'tax_query' => array(
                    array(
                        'taxonomy' => 'product_visibility',
                        'field'    => 'name',
                        'terms'    => 'featured',
                    ),
                ),
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
                wc_get_template_part( 'content', 'product' );
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>
    

    WooCommerce 3中的特色产品循环

    <ul class="products">
    <?php
        $args = array(
            'post_type'      => 'product',
            'posts_per_page' => 12,
            'orderby'        => 'rand',
            'tax_query' => array(
                    array(
                        'taxonomy' => 'product_visibility',
                        'field'    => 'name',
                        'terms'    => 'featured',
                    ),
                ),
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
                wc_get_template_part( 'content', 'product' );
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>
    

    尝试了此操作,但无效。我也尝试了另一种方法,将阵列向上放置一层,而不是向下放置一层。这也不管用。还尝试了参数“meta_key”=>“\u featured”。也不起作用。尝试了这个,但不起作用。我也尝试了另一种方法,将阵列向上放置一层,而不是向下放置一层。这也不管用。还尝试了参数“meta_key”=>“\u featured”。也不起作用。非常感谢。这就是解决办法。现在开始工作了!我已经在$query_参数中添加了'orderby'=>'rand',这样就可以随机显示其中一个特色产品。适用于woocommerce 3.x-如果能够为特色产品提供诸如'wc_get_product_ids_on_sale()'之类的功能,那就太好了。非常感谢。这就是解决办法。现在开始工作了!我已经在$query_参数中添加了'orderby'=>'rand',这样就可以随机显示其中一个特色产品。适用于woocommerce 3.x-最好能为特色产品提供功能,如:'wc_get_product_ids_on_sale()'