如何在 wordpress 中按年月排序的嵌套列表中添加 post 缩略图

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

我的存档页面中有一个列表,其中显示 post 首先按年排序,然后按月排序。如何添加带有 post 标题的 post 缩略图?我对 php 和 wordpress 了解不多。这是列表的代码。希望有人能帮助我。

<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 具有可用于获取 post 缩略图的函数,所以像这样:

<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>

函数get_the_post_thumbnail returns HTML 缩略图,所以你只需要在正确的地方回显它。