php - archive per year with monthly

I have an archive that shows all the post on monthly now i want is to group them per year like2015 JanFebMarch2014April

I have an archive that shows all the post on monthly now i want is to group them per year like

2015 
 Jan
 Feb
 March
2014
 April
 May

I'm using this code.

<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>

This displays all the archive monthly from the beginning. i tried to add a monthly and yearly but they are on a separate group...

I have an archive that shows all the post on monthly now i want is to group them per year like

2015 
 Jan
 Feb
 March
2014
 April
 May

I'm using this code.

<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>

This displays all the archive monthly from the beginning. i tried to add a monthly and yearly but they are on a separate group...

Share Improve this question asked Jun 16, 2015 at 2:38 MIkeMIke 8311 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

To display monthly archives arranged by year, you can use this code. It checks through each month if WordPress have any post in that month and create archive.

<?php
    global $wpdb;
    $limit = 0;
    $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 ) { ?>
            <?php } ?>
            <h3><a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a></h3>
        <?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_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a></li>
        <?php $year_prev = $year_current;
        if( ++$limit >= 18 ) { break; }
    endforeach;
?>
<?php
    global $wpdb;
    $limit = 0;
    $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 = 'newses' 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 ) { ?>
            <?php } ?>
            <dt><i class="fa fa-fw fa-plus-square-o" aria-hidden="true"></i><a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a></dt>
        <?php } ?>
        <dd class="post_month"><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_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a></dd>
        <?php $year_prev = $year_current;
        if( ++$limit >= 18 ) { break; }
    endforeach;
?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744854223a4597309.html

相关推荐

  • php - archive per year with monthly

    I have an archive that shows all the post on monthly now i want is to group them per year like2015 JanFebMarch2014April

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信