Post List Showing Two Current Years
-
Guys,
I’m trying to display a simple linkable list of posts for two years only. Per Mikos at this post he made code that uses a single DB call to grab the info. Below is the code.
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; }
he also displays this block of code and explains that it should be inserted into the theme.
<?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; ?>
How can this be modified so a shortcode can be used? I’m confused as to how to insert the second half of the code in the template if it is to only be shown on one page.
Thanks in advance for any help.
FYI: The theme used is Genesis. All plugins and core are up to date.
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Post List Showing Two Current Years’ is closed to new replies.