wordpress - Wordpress:我想在 wordpress 的页面上显示所有类别的最新产品?
问题描述
我希望在 wordpress 的页面上显示所有类别中的最新一个产品,当我们在其中添加更多类别和产品时,它应该必须在页面上添加(显示)其单个最新产品。我们该怎么做?请帮我。谢谢
解决方案
首先,您需要使用hide_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>";
}
}
推荐阅读
- docker - 启用在 docker 镜像中调用 webhook
- java - 字符串中单词之间额外空格的正则表达式条件
- php - 如何编写多个 .htaccess 重写规则?
- c++ - 在消息映射中创建按钮和引用
- javascript - 同位素 - jQuery - 内容更改后图像相互堆叠
- android-studio - Android Studio 3.3 中缺少 Instant Run
- python-2.7 - 从 dataset.variable 返回的 OrderedDict()
- eiffel - 埃菲尔:参考其他类常量设置常量
- laravel - 雄辩的独特选择 laravel
- apache-kudu - 如何使用 Apache Kudu 获取一系列行(例如第 1000~2000 行)?