我在wordpress中有类似这样的帖子 类别“第一” 2019-发布1 2019年-第2期 2019-后3 2018年-Post1 2018-发布2
如何显示这样的内容
类别“第一”
2019
帖子1
帖子2
Post3
2018
帖子1
帖子2
现在,这是我拥有的代码,用于呈现给定类别“眼镜”的帖子。
<?php
$args = array(
'post_type'=> 'post',
'category_name'=>'spectacle',
'posts_per_page'=> -1
);
$specQuery = new WP_Query ($args);
while ($specQuery->have_posts()): $specQuery->the_post();
?>
<div class="row porfolio-details">
<div class="portfolio-image">
<?php echo the_post_thumbnail( 'full' ); ?>
<div class="caption">
<h6><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
</div>
</div>
<div class="portfolio-text">
<h6>
<?php the_content();?>
</h6>
</div>
</div>
<!--ENDS AND RESET THE LOOP-->
<?php endwhile; wp_reset_query(); ?>
那么如何遍历类别并通过另一个参数,使该参数仅显示一次?
答案 0 :(得分:0)
请替换为以下代码:
<?php
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY year DESC" );
foreach ( $years as $year ) {
echo '<h2>' . $year->year . '</h2><ul>';
$args = array(
'post_type'=> 'post',
'category_name'=>'spectacle',
'posts_per_page'=> -1
);
$specQuery = new WP_Query ($args);
while ($specQuery->have_posts()): $specQuery->the_post();
?>
<div class="row porfolio-details">
<div class="portfolio-image">
<?php echo the_post_thumbnail( 'full' ); ?>
<div class="caption">
<h6><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
</div>
</div>
<div class="portfolio-text">
<h6>
<?php the_content();?>
</h6>
</div>
</div>
<!--ENDS AND RESET THE LOOP-->
<?php endwhile; wp_reset_query(); ?>
<?php } ?>