Get posts by years in WordPress
Get posts by years in WordPress
<?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC"); ?>
<?php foreach($years as $year) : ?>
<h2><?php echo $year; ?></h2>
<ul>
<?php query_posts('year='.$year); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>
</ul>
<?php endforeach; ?>
Post a Comment