Wordpress:我想在Wordpress页面上显示所有类别的最新产品?

Wordpress:我想在Wordpress页面上显示所有类别的最新产品?,wordpress,Wordpress,我希望所有类别中的最新产品都显示在wordpress的页面上,当我们在其中添加更多类别和产品时,应该在页面上添加(显示)单个最新产品。我们如何做到这一点?请帮帮我。谢谢首先,您需要使用hide\u empty获取至少一篇文章中的所有产品类别 然后循环遍历每个类别并对每个类别运行查询,以获得单个产品 $args = array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => t

我希望所有类别中的最新产品都显示在wordpress的页面上,当我们在其中添加更多类别和产品时,应该在页面上添加(显示)单个最新产品。我们如何做到这一点?请帮帮我。谢谢

首先,您需要使用
hide\u empty
获取至少一篇文章中的所有产品类别

然后循环遍历每个类别并对每个类别运行查询,以获得单个产品

$args = array(
    'orderby'    => 'name',
    'order'      => 'ASC',
    'hide_empty' => true
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
    foreach ( $product_categories as $product_category ) {
        echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
        $args = array(
            'posts_per_page' => 1,
            'post_status' => 'publish',
            'post_type' => 'product',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    'terms' => $product_category->slug
                )
            ),

        );
        $products = new WP_Query( $args );
        echo "<ul>";
        while ( $products->have_posts() ) {
            $products->the_post();
            ?>
                <li>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                </li>
            <?php
        }
        wp_reset_postdata();
        echo "</ul>";
    }
}
$args=array(
'orderby'=>'name',
“订单”=>“ASC”,
“hide_empty”=>true
);
$product\U CATEGORES=获取术语('product\U cat',$args);
$count=计数($product\u categories);
如果($count>0){
foreach($product\U CATEGORES作为$product\U category){
回声';
$args=数组(
“每页帖子数”=>1,
“发布状态”=>“发布”,
“post_类型”=>“产品”,
“tax_query”=>数组(
'关系'=>'和',
排列(
“分类法”=>“产品分类”,
'字段'=>'段塞',
“术语”=>$product\u类别->slug
)
),
);
$products=新的WP_查询($args);
回声“
    ”; 而($products->have_posts()){ $products->the_post(); ?>

  • 您好Jainil先生,非常感谢您的宝贵反馈,先生,我应该将这些代码粘贴到哪里,我是新手wordpress@AkshayShelke在您的模板文件中,您希望在其中显示产品列表。谢谢您,先生,是的,我已经完成了,您的答案非常简单helpfull@AkshayShelke如果答案对您有效,请将其标记为已批准。