在模板页面上通过WooCommerce中的定制循环展示特色产品

时间:2015-07-23 作者:steamfunk

我想在我的主页上展示我的woocommerce商店的6种特色产品。php模板。经过一些研究,我发现正确的方法是通过自定义循环(我不希望使用短代码,因为我想为样式添加其他类等),我还发现woocommerce用于特色产品的关键是“u特色”。我把下面的代码放在一起,以显示我选择作为商店特色产品的任何产品,但它不起作用。。。非常感谢您的帮助。

<?php

    $args = array(
        \'post_type\'   => \'product\',
        \'stock\'       => 1,
        \'showposts\'   => 6,
        \'orderby\'     => \'date\',
        \'order\'       => \'DESC\' ,
        \'meta_query\'  => array(
            array(
                \'key\'     => \'_featured\',
                \'value\'   => 0,
                \'compare\' => \'>\',
                \'type\'    => \'numeric\'
            )
        )
    );

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

        <li>    
            <?php 
                if ( has_post_thumbnail( $loop->post->ID ) ) 
                    echo get_the_post_thumbnail( $loop->post->ID, \'shop_catalog\' ); 
                else 
                    echo \'<img src="\' . woocommerce_placeholder_img_src() . \'" alt="Placeholder" width="65px" height="115px" />\'; 
            ?>
            <h3><?php the_title(); ?></h3>

            <?php 
                echo $product->get_price_html(); 
                woocommerce_template_loop_add_to_cart( $loop->post, $product );
            ?>    
        </li>

<?php 
    endwhile;
    wp_reset_query(); 
?>

10 个回复
最合适的回答,由SO网友:Hooman Askari 整理而成

将args更改为如下所示:

$meta_query   = WC()->query->get_meta_query();
$meta_query[] = array(
    \'key\'   => \'_featured\',
    \'value\' => \'yes\'
);
$args = array(
    \'post_type\'   =>  \'product\',
    \'stock\'       =>  1,
    \'showposts\'   =>  6,
    \'orderby\'     =>  \'date\',
    \'order\'       =>  \'DESC\',
    \'meta_query\'  =>  $meta_query
);
如果你去wp content/plugins/woocommerce/includes/class wc shortcodes。php(@595)你可以找到如何使用WC短代码。

SO网友:dpruth

这在WooCommerce 3.0中发生了变化。它不仅仅是一个meta\\u查询,现在还包括一个tax\\u查询。这些参数现在是:

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

    $args = array(
        \'post_type\'           => \'product\',
        \'post_status\'         => \'publish\',
        \'ignore_sticky_posts\' => 1,
        \'posts_per_page\'      => $atts[\'per_page\'],
        \'orderby\'             => $atts[\'orderby\'],
        \'order\'               => $atts[\'order\'],
        \'meta_query\'          => $meta_query,
        \'tax_query\'           => $tax_query,
    );
请参阅woocommerce/includes/class wc短代码。php

SO网友:Mostafa Norzade

Featured Products Loop in WooCommerce 3

<ul class="products">
<?php
$args = array(
    \'post_type\' => \'product\',
    \'posts_per_page\' => 12,
    \'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();
?>
SO网友:cfx

根据WooCommerce Wiki:

Building custom WP_Queries or database queries [to retrieve products] is likely to break your code 在未来版本的WooCommerce中,随着数据向定制表移动,以获得更好的性能。

WooCommerce提倡使用wc_get_products()WC_Product_Query() 而不是WP_Query()get_posts().

我写了一篇帖子,其中包含了我用来实现您在这里所需的代码:https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/

SO网友:Felipe Elia

我知道这很古老,但我刚刚分享了一个替代解决方案here 我认为它也可以帮助那些达到这个主题的人。

而不是使用meta_querytax_query, 您可以使用wc_get_featured_product_ids() 也是:

$args = array(
    \'post_type\'           => \'product\',
    \'posts_per_page\'      => 6,
    \'orderby\'             => \'date\',
    \'order\'               => \'DESC\',
    \'post__in\'            => wc_get_featured_product_ids(),
);

$query = new WP_Query( $args );
我希望这有帮助!

SO网友:conschneider

依据:https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query

我会尝试:

外部回路:

$args = array (
\'limit\' => 6,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'category\' => $club_cat,
\'stock_status\' => \'instock\',
\'featured\' => true,

 );

 $products = wc_get_products( $args );
在循环中:

$query = new WC_Product_Query( array(
\'limit\' => 6,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'category\' => $club_cat,
\'stock_status\' => \'instock\',
\'featured\' => true,
\'return\' => \'ids\',

 ) );

 $products = $query->get_products();

SO网友:jameshwart lopez

人们应该开始使用wc_get_产品,因为woocommerce表示这是检索产品的标准方式。wc_get_products有一个参数,只需要设置为true。所以代码就在下面。

 <?php 

// Display featured products by category. on this case its "shirts" which is the slug of the category.
$query_args = array(
    \'featured\' => true,  
    \'category\' => array( \'shirts\' ),
);
$products = wc_get_products( $query_args );

global $post;
$columns = wc_get_loop_prop( \'columns\' );
?>
<div class="woocommerce columns-<?php echo esc_attr( $columns ); ?>">
  <?php
    woocommerce_product_loop_start();
    foreach ($products as $product) {
        $post = get_post($product->get_id());
        setup_postdata($post);
        wc_get_template_part(\'content\', \'product\');
    }
    wp_reset_postdata();
    woocommerce_product_loop_end();
  ?>
</div>
See full post here:https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/

SO网友:Aamer Shahzad

如果您查看中的数据库wp_postmeta 您将看到的表格meta_key 将是_featuredmeta_value 将是yesno 所以不是价值01yesno

<?php
    $q = new WP_Query([
      \'post_type\'   =>  \'product\',
      \'stock\'       =>  1,
      \'showposts\'   =>  3,
      \'orderby\'     =>  \'date\',
      \'order\'       =>  \'DESC\',
      \'meta_query\'  =>  [ 
        [\'key\' => \'_featured\', \'value\' => \'yes\' ]
        ]
    ]);
    if ( $q->have_posts() ) :
        while ( $q->have_posts() ) : $q->the_post();
            // display product info
        endwhile; wp_reset_query();
    endif;
?>

SO网友:Dhaval
<ul class="products">
    <?php
        $args = array(
            \'post_type\' => \'product\',
            \'posts_per_page\' => 12,
            \'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();
               echo \'<p>\'.get_the_title().\'</p>\';
            endwhile;
        } else {
            echo __( \'No products found\' );
        }
        wp_reset_postdata();
    ?>
</ul><!--/.products-->
SO网友:Chad Reitsma

2021的通气-您需要使用wc_get_products()

另外,Jameshwart(上面)的代码示例对我不起作用,我就是这样设置postdata&;使用wc\\u get\\u模板。已测试(&A);工作

$args = array(
    \'status\' => \'publish\',
    \'limit\' => 20,
    \'paginate\' => true, //must be true in order to get ->total
    \'return\' => \'ids\',
);
$products = wc_get_products( $args );

if ($products->total >= 1):

    woocommerce_product_loop_start();

    foreach($products->products as $pid):

        do_action( \'woocommerce_shop_loop\' );

        $product = get_post($pid);
        setup_postdata($GLOBALS[\'post\'] =& $product);
        wc_get_template_part(\'content\', \'product\');

    endforeach;
    wp_reset_postdata();

    woocommerce_product_loop_end();


endif; //we have products

结束

相关推荐

301 Redirect Loop

我有wordpress网站托管在openshift上,比如说。com。我正在重定向示例。com到www.example。com(非www到www)。我已经为www和非www版本创建了别名,并用app name更新了我的cname。我试着将非www cname记录指向www版本,但除了主页(相同的301重定向循环)外,它仍在工作,所以我将非www cname还原回应用程序url。现在www版本正在提供服务,但非www版本正在通过重定向到自身来创建301重定向循环。我的wordpress网站url是www.e