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)
1 Answer
The function get_the_time
is only designed to work either in the main WordPress loop or a custom loop using WP_Query
, so the way you are using it won’t work correctly. The following is the right way to use it, using a custom WP_Query
loop:
<?php
// Get years that have posts
$years = $wpdb->get_results( "SELECT DISTINCT YEAR(post_date) AS year FROM $wpdb->posts WHERE post_type="post" AND post_status="publish" ORDER BY year ASC;" );
// For each year, do the following
foreach ( $years as $year ) {
// Get all posts for the year. Using WP_Query instead of custom mySQL
$posts_this_year = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'year' => $year->year
) );
// Display the year as a header
echo '<h1>' . $year->year . '</h1>';
// Start an unorder list
echo '<ul class="posts-in-year">';
// As long as you have posts for that year, do the following. Using the Loop. get_the_date now works
while ( $posts_this_year->have_posts() ) {
//Makes current post available to template tag functions like get_the_time
$posts_this_year->the_post();
// Display the title as a hyperlinked list item
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>' . ' ' . '—' . ' ' . get_the_time('F j') . '</li>';
}
//Reset post data. Important to do this so not to mess with main loop
wp_reset_postdata();
// End the unordered list
echo '</ul>';
}
?>
The Loop, which I was referring to earlier, is when you use WP_Query
to iterate through the posts for you. When you do it that way, functions like get_the_time
work properly because they can pull the info for the current post in the loop. For more information, read The codex data on WP_Query
and multiple loops