archive per year with monthly

时间:2015-06-16 作者:MIke

我有一个档案,显示每月的所有帖子,现在我想每年对它们进行分组,就像

2015 
 Jan
 Feb
 March
2014
 April
 May
我正在使用此代码。

<?php wp_get_archives( array( \'type\' => \'monthly\' ) ); ?>
这将从一开始每月显示所有存档。我试图添加每月和每年一次,但他们是在一个单独的组中。。。

2 个回复
SO网友:Robert hue

要显示按年度排列的每月存档,可以使用此代码。它每月检查WordPress是否有任何帖子,并创建存档。

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

SO网友:Biplab Chakraborty
<?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;
?>
结束

相关推荐

如何解决1145行/wp-includes/capabilities.php中的通知:未定义的偏移量:0

我已经看过这个问题的以前的例子,但根据我从其他人那里看到的情况,我找不到导致这个通知的解决方案。当我添加新帖子时,这会出现在屏幕顶部。1145是:$post = get_post( $args[0] ); 我没有收到任何其他类型的错误,因此我不确定这是在我的代码中导致问题的地方。Any help on this?代码如下://show metabox in post editing page add_action(\'add_meta_boxes\', \'kk_add_metabox\