如何在wordpress中按年份和月份在嵌套列表中添加文章缩略图


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

我的存档页面中有一个列表,其中显示的帖子先按年份排序,然后按月份排序。如何将文章缩略图与文章标题一起添加?我对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有一些功能可以用来获取帖子缩略图,所以类似这样的东西:

<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返回缩略图的HTML,所以您只需要在正确的位置回显它。