How to add post thumbnail in a nested list ordered by year and month in wordpress

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>

Related posts

Leave a Reply

1 comment

  1. WordPress has functions you can use to get the post thumbnail, so something like this:

    <li>
        <a href="<?php bloginfo('url'); ?>?p=<?php echo $theid->ID; ?>">
        <h2><?php echo $theid->post_title; ?></h2>
        <?php echo get_the_post_thumbnail( $theid->ID, 'post-thumbnail' ); ?>
        </a></li>
    

    The function get_the_post_thumbnail returns the HTML for the thumbnail image, so you just need to echo it in the right place.