Php Wordpress-如何仅显示存档列表中有帖子的年份和月份

Php Wordpress-如何仅显示存档列表中有帖子的年份和月份,php,wordpress,archive,Php,Wordpress,Archive,我有一个存档列表,它按年度和月份显示帖子。但它显示了没有帖子的岁月和月份。我如何才能只显示实际有帖子的月份和年份?多谢各位 <?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC"); ?> <?php foreach($years as $ye

我有一个存档列表,它按年度和月份显示帖子。但它显示了没有帖子的岁月和月份。我如何才能只显示实际有帖子的月份和年份?多谢各位

<?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM    $wpdb->posts WHERE  post_status = 'publish' ORDER BY post_date DESC"); ?>

<?php foreach($years as $year) : ?>
<span class="anni"><?php echo $year; ?></span>
        <ul style="list-style-type:none;"> 
        <?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"); ?>

        <?php foreach($months as $month) : ?>

        <li><h3 class="mesi"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></h3>
            <ul> 

            <?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"); ?>

                <?php foreach ($theids as $theid): ?>
            <li class="archivelist">


                    <div style="width:150px;height:170px;float:left;margin:10px;">
            <a href="<?php bloginfo('url'); ?>?p=<?php echo $theid->ID; ?>">

            <?php if (strlen($theid->post_title) > 15) :?>
                <?php echo substr($theid->post_title, 0, 15) . '...'; ?>
            <?php else : ?>
                <?php echo $theid->post_title; ?>
            <?php endif; ?>

            <?php if (has_post_thumbnail($theid->ID) ) : ?>
                    <?php echo get_the_post_thumbnail( $theid->ID, 'thumbnail' ); ?>
            <?php else : ?>
                <?php echo '<img style="width:150px;" src="wp-content/themes/twentythirteen-child/images/noimg.jpg"/>' ;?>
            <?php endif; ?>
                </a>

            </div>
            </li>
                <?php endforeach; ?>

            </ul>   <br style="clear:left;"> 

        </li>

        <?php endforeach; ?>

        </ul>


    <?php endforeach; ?>


    显然,在几个月内,除了“post”之外,还有其他类型的帖子不应该显示

    尽量包括

    AND post_type = 'post'
    
    进入查询中用于选择年份和月份的WHERE子句

    我就是这样做的:

     SELECT JAL.year, JAL.month, COUNT(JAL.ID) as `posts` FROM (" .
            "SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`,ID " .
            "FROM {$wpdb->posts} {$join} {$where}" .
     ") JAL GROUP BY JAL.year,JAL.month ORDER BY JAL.year,JAL.month DESC
    
    你对所有的帖子进行选择,然后将其年份和月份提取出来,然后按年份和月份对它们进行分组