如何显示年/月档案,包括无帖子的月份

时间:2014-02-12 作者:TJ Sherrill

我在以下位置找到了部分答案:http://floatleft.com/notebook/wordpress-year-month-archives/

此解决方案显示所有有帖子的月份。甚至没有那么多代码:

<?php
        $year_prev = null;
        $months = $wpdb->get_results(   "SELECT DISTINCT MONTH( post_date ) AS month ,
                                        YEAR( post_date ) AS year,
                                        COUNT( id ) as post_count FROM $wpdb->posts
                                        WHERE post_status = \'publish\' and post_date <= now( )
                                        and post_type = \'post\'
                                        GROUP BY month , year
                                        ORDER BY post_date DESC");
        foreach($months as $month) :
            $year_current = $month->year;
            if ($year_current != $year_prev){
                if ($year_prev != null){?>
                </ul>
                <?php } ?>
            <h3><?php echo $month->year; ?></h3>
            <ul class="archive-list">
            <?php } ?>
            <li>
                <a href="<?php bloginfo(\'url\') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>">
                    <span class="archive-month"><?php echo date("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span>
                </a>
            </li>
        <?php $year_prev = $year_current;
        endforeach; ?>
        </ul>
我的问题是,设计师添加了所有的月份,而不仅仅是那些有帖子的月份。我如何编辑它来显示所有月份,而不仅仅是有帖子的月份。我正在使用3.8.1

谢谢

2 个回复
最合适的回答,由SO网友:gdaniel 整理而成

下面的代码将在每年的每个地方循环,检查每个月是否有帖子。如果是这样,它将添加一个月的链接。如果没有,它将只打印月份,没有链接。你需要编辑实际链接,因为我不确定你网站的永久链接结构。

//Grab the earliest year available
    $yearliest_year = $wpdb->get_results(

        "SELECT YEAR(post_date) AS year 
         FROM $wpdb->posts 
         WHERE post_status = \'publish\' 
         AND post_type = \'post\'
         ORDER BY post_date 
         ASC LIMIT 1

    ");


    //If there are any posts
    if($yearliest_year){

        //This year
        $this_year = date(\'Y\');

        //Setup months
        $months = array(1 => "January", 2 => "February", 3 => "March" , 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");

        $current_year = $yearliest_year[0]->year;


        //Loop through every year and check each monnth of each year for posts
        while($current_year <= $this_year){

            echo "<h3>" . $current_year . "</h3>";

            echo "<ul>";

            foreach($months as $month_num => $month){


                //Checks to see if a month a has posts
                if($search_month = $wpdb->query(

                        "SELECT MONTHNAME(post_date) as month 

                            FROM $wpdb->posts  
                            WHERE MONTHNAME(post_date) = \'$month\'
                            AND YEAR(post_date) = $current_year 
                            AND post_type = \'post\'
                            AND post_status = \'publish\'
                            LIMIT 1 

                ")){

                    //Month has post -> link it
                    echo "<li>

                            <a href=\'" . get_bloginfo(\'url\') . "/" . $current_year . "/" . $month_num . "/\'><span class=\'archive-month\'>" . $month . "</span></a>

                          </li>";


                }else{

                    //Month does not have post -> just print it

                    echo "<li>

                            <span class=\'archive-month\'>" . $month . "</span>

                          </li>";
                }



            }

            echo "</ul>";

            $current_year++;


        }

    }else{

        echo "No Posts Found.";

    }

SO网友:sakibmoon

一种解决方案是增加缺失的月份。您必须检查是否存在一年中的所有月份。如果不是,则将该月添加为post_count

<?php
        $year_prev = null;
        $months = $wpdb->get_results(   "SELECT DISTINCT MONTH( post_date ) AS month ,
                                        YEAR( post_date ) AS year,
                                        COUNT( id ) as post_count FROM $wpdb->posts
                                        WHERE post_status = \'publish\' and post_date <= now( )
                                        and post_type = \'post\'
                                        GROUP BY month , year
                                        ORDER BY post_date DESC");

        function month_exist($month, $year, $objects){
            foreach($objects as $object){
                if( ($object->month == $month) && ($object->year == $year) )
                return true;
            }
            return false;
        }

        $all_month = range(1,12);
        $new_array = $months;
        $i = 0;
        foreach($months as $month){
            $year_current = $month->year;
            if( $year_current != $year_prev ){
                foreach( $all_month as $current_month ){
                    if( !month_exist($current_month, $month->year, $months)){
                        $value = new StdClass();
                        $value->month = $current_month;
                        $value->year = $month->year;
                        $value->post_count = 0;
                        array_splice($new_array, $i*12+($current_month-1), 0, array($value));
                    }
                }
                $i++;
            }
            $year_prev = $month->year;
        }

        $months = $new_array;
        $year_prev = null;
        foreach($months as $month) :
            $year_current = $month->year;
            if ($year_current != $year_prev){
                if ($year_prev != null){?>
                </ul>
                <?php } ?>
            <h3><?php echo $month->year; ?></h3>
            <ul class="archive-list">
            <?php } ?>
            <li>
                <a href="<?php bloginfo(\'url\') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>">
                    <span class="archive-month"><?php echo date("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span>
                </a>
            </li>
        <?php $year_prev = $year_current;
        endforeach; ?>
        </ul>

结束

相关推荐

显示Archives.php中的所有自定义帖子类型

我该怎么做?archive.php 只有以下内容:wp_get_archives(\'type=monthly\'); 以及wp_get_archives() 没有显示所有帖子类型的参数。我也认为archive-[post_type].php 不是我要找的,因为我希望所有帖子类型都显示在一个归档页面中。谢谢W