代码之家  ›  专栏  ›  技术社区  ›  giulio maione

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

  •  1
  • giulio maione  · 技术社区  · 9 年前

    我的档案页面中有一个列表,显示了先按年份排序,然后按月份排序的帖子。如何将文章缩略图与文章标题一起添加?我不太了解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 回复  |  直到 9 年前
        1
  •  1
  •   user488187 user488187    9 年前

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