我怎么能每年只循环几个月呢?

时间:2020-04-02 作者:Fernando Souza

我有一个Wordpress归档栏,每年都会循环发布帖子,显示年份和月份。

The output should look like this: //year output

2020 //month output
month1
month2
month3
2019
month1
month2
month3
month4
问题是:它每年都会显示几个月,我想限制每年都有帖子的mounth的循环。

这是我失败的循环:

foreach($months as $month) : $year_current = $month->year;
以下是全部代码:

<ul class="side-list">
                                <!-- year -->
                                <?php
                                    global $wpdb;
                                    $limit = 0;
                                $year_prev = null;
                                $current_post_type = get_post_type();
                                $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 = \'$current_post_type\' GROUP BY month , year ORDER BY post_date DESC");

                                foreach($months as $month) : $year_current = $month->year;
                                    if ($year_current != $year_prev){
                              ?>
                                <li class="side-item">
                                    <h3 class="side-sub-element-ttl js-toggle-menu-02">
                                        <?php
                                                $args = array(
                                                        \'post_type\' => $current_post_type,
                                                        \'date_query\' => array(
                                                                \'year\'  => $month->year

                                                        )
                                                );
                                                $year_query = new WP_Query($args);
                                        ?>
                                        <?php echo $month->year; ?>年
                                    </h3>
                                    <div class="js-toggle-content">
                                        <!-- months -->
                                        <ul class="side-sub-list flex-row">
                                            <?php
                                                foreach($months as $month) : $year_current = $month->year;
                                            ?>
                                            <li class=\'list-group-item month\'>
                                        <a href="<?php bloginfo(\'url\') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>/?post_type=<?php echo $current_post_type ?>">
                                           <span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span>
                                        </a>
                                        </li>
                                            <?php
                                            endforeach;
                                            ?>
                                    </ul>
                                        <!-- end of months -->
                                    </div>
                                </li>
                            <?php } ?>
                            <?php
                            $year_prev = $year_current;
                            endforeach;
                            ?>
                            <!-- end of year -->

                            </ul>

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

我已经试过你的问题了。它以正确的方式播放。我认为问题在于你在foreach循环中对今年的处理。

我想你想达到这个效果,对吧?

// it is the test result based on some dummy posts
2020
 March
2000
 October
1999
 March
 January
1986
 September
1981
 November

这里有一个简化的版本来说明迭代。

<?php

// your original query, the result object is in right structure for iteration
global $wpdb;
$year_prev = null;
$current_post_type = get_post_type();
$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 = \'$current_post_type\' GROUP BY month , year ORDER BY post_date DESC");

// iterate the result
$current_year = \'\'; // for first time
foreach($months as $month) : 
    // if it is first time or it is not the same as before
    // echo a close list (for 2nd time or after, must do it before assign a new year)
    // assign a new year
    if( empty( $current_year ) || $current_year != $month->year ) {
        if( ! empty( $current_year ) ) {
            echo \'</ul>\';
        }

        $current_year = $month->year;
        echo \'<h3>\' . $month->year . \'年</h3>\';
        echo \'<ul class="side-list">\';
    }

    // use the internal locale function to get a named month from the number
    echo  \'<li>\' . $wp_locale->get_month( $month->month ). \'</li>\';
endforeach;
?>

相关推荐

permalinks issue and archives

我对运行在WP 3.3上的一个站点有一个问题,当我们通过“/%post\\u id%/%postname%/”使永久链接成为任何内容时,归档页面会断开并变成404。经过一些研究,我明白了为什么从性能的角度来看,这不是一个好的做法,所以我尝试了建议的备选方案:“/%year%/%postname%/”和“/%post\\u id%/%postname%/”这两个建议都有效,只是只有使用post\\u id的建议,归档URL才会变成“/date/2012/11/”,并被找到。根据permalink的任何其他建