I have a list in my archive page that shows posts ordered firstly by years and then months. How can I add the post thumbnail too with the post title? I don’t know much about php and wordpress. Here’s the code of the list. Hope someone can help me.
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<ul id="archivio"> <!--anni-->
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC");
foreach($years as $year) : ?>
<li><?php echo $year; ?>
<ol class="mesi"> <!--mesi-->
<?php $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
foreach($months as $month) : ?>
<li>
<?php echo date( 'F', mktime(0, 0, 0, $month) );?>
<ul class="post"> <!--post-->
<?php $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND MONTH(post_date)= '".$month."' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
foreach ($theids as $theid): ?>
<li><a href="<?php bloginfo('url'); ?>?p=<?php echo $theid->ID; ?>"><?php echo $theid->post_title; ?>
</a></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach;?>
</ol>
</li>
<?php endforeach; ?>
</ul>
</div>
WordPress has functions you can use to get the post thumbnail, so something like this:
The function get_the_post_thumbnail returns the HTML for the thumbnail image, so you just need to echo it in the right place.