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

时间:2015-02-18 16:30:39

标签: php wordpress list thumbnails

我的档案页面中有一个列表,显示按年份和月份排序的帖子。如何使用帖子标题添加帖子缩略图?我不太了解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>

1 个答案:

答案 0 :(得分:1)

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,因此您只需要在正确的位置回显它。