最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

archives - How can I loop only months foreach year?

programmeradmin5浏览0评论

I have a Wordpress archive bar that loops foreach year that have posts, displaying the years and months.

The output should look like this: //year output

2020 //month output
month1
month2
month3
2019
month1
month2
month3
month4

The problem: Its displaying months in every year, I want to limit my loop for mounths that have posts foreach year.

Here is my failed loop:

foreach($months as $month) : $year_current = $month->year;

Here is the whole code:

<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>

I have a Wordpress archive bar that loops foreach year that have posts, displaying the years and months.

The output should look like this: //year output

2020 //month output
month1
month2
month3
2019
month1
month2
month3
month4

The problem: Its displaying months in every year, I want to limit my loop for mounths that have posts foreach year.

Here is my failed loop:

foreach($months as $month) : $year_current = $month->year;

Here is the whole code:

<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>
Share Improve this question edited Apr 2, 2020 at 7:16 Chetan Vaghela 2,4084 gold badges10 silver badges16 bronze badges asked Apr 2, 2020 at 7:10 Fernando SouzaFernando Souza 1512 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I have tried your query. It plays in right way. I think the problem is your handling of current year in the foreach loop.

I suppose you would like to achieve this effect, right?

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

Here is a simplified version to illustrate the iteration.

<?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;
?>
发布评论

评论列表(0)

  1. 暂无评论