Okay, so I'm making the blog page for my WordPress site. My front page is displayed as static, so my blog posts are on another page called "Journal." I want to display the posts in an archive-like fashion. I'm trying to achieve the following:
I researched how to do this, and I found a way; however, it displays the dates for all the posts as "November 30."
This is the code I'm using:
<?php
// Get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY year ASC" );
// For each year, do the following
foreach ( $years as $year ) {
// Get all posts for the year
$posts_this_year = $wpdb->get_results( "SELECT ID, post_title FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
// Display the year as a header
echo '<h1>' . $year->year . '</h1>';
// Start an unorder list
echo '<ul class="posts-in-year">';
// For each post for that year, do the following
foreach ( $posts_this_year as $post ) {
// Display the title as a hyperlinked list item
echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a>' . ' ' . '—' . ' ' . get_the_time('F j') . '</li>';
}
// End the unordered list
echo '</ul>';
}
?>
Basically, I'm trying to get the time from each post and display its month and day (F j)
WP_Query loop
.get_the_time
is only designed to work either in the main WordPress loop or a custom loop usingWP_Query
$years
? I'm unfamiliar with$wpdb
.get_the_time