List posts by year, 6 per page.
-
I’ve managed to get and list my posts by year, but
Code for listing posts by year:
functions.php
function posts_by_year() { // array to use for results $years = array(); // get posts from WP $posts = get_posts(array( 'numberposts' => -1, 'orderby' => 'post_date', 'order' => 'ASC', 'post_type' => 'post', 'post_status' => 'publish' )); // loop through posts, populating $years arrays foreach($posts as $post) { $years[date('Y', strtotime($post->post_date))][] = $post; } // reverse sort by year krsort($years); return $years; }
template file
<?php foreach(posts_by_year() as $year => $posts) : ?> <h2><?php echo $year; ?></h2> <ul> <?php foreach($posts as $post) : setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; ?> </ul> <?php endforeach; ?>
What should I do next so that only 6 posts per page are shown?
Anyone could give me a hint here?
Thanks.
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘List posts by year, 6 per page.’ is closed to new replies.