douzhi9939 2014-11-18 08:25
浏览 46
已采纳

需要WordPress存档以某种格式显示

my WordPress blog currently shows archives in a month by month format. Like this:

November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
.....
and so on

I need it to display in such a way that the current year shows all months, but for all the years before that, it only shows the "Year". Something like this:

November 2014
October 2014
September 2014
...
...
February 2014
January 2014
2013
2012
2011
2010

Can someone please guide me on this? The current code in my blog's sidebar goes like this:

<?php /* If this is a category archive */ if ( is_category(0) || in_category(0)) { ?>
<?php ?><ul>
<?php
$querystr = "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month' , count(ID) as posts FROM $wpdb->posts INNER JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE $wpdb->term_taxonomy.term_id != 12 AND $wpdb->term_taxonomy.parent != 12 AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id =8 AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";

$years = $wpdb->get_results($querystr);

foreach ( (array) $years as $year ) {
    $url = get_month_link($year->year, $year->month );
    $url = $url.'?cat=8';
    $date = mysql2date('F o', $year->year.'-'.$year->month, $translate = true);
    echo get_archives_link($url, $date, 'html','<li>','</li>');
}
?>
</ul><?php 

?>

<?php } else { ?>
            <?php wp_get_archives('type=monthly');?>

<?php } ?>



                </ul>






                         </div>
                           <div class="archive-bottom"></div>
                <!--    <div class="clear"></div>-->
 <?php } ?>                   




</div>
  • 写回答

1条回答 默认 最新

  • dongmale0656 2014-11-18 09:26
    关注

    You're currently using nothing other than wp_get_archives('type=monthly'), which is obviously causing the issue. You should have a look at the wp_get_archives() docs in the Codex.

    The following is a sample solution, but will likely need a bit of work (it assumes you're posting every month this year, as shown in your post). It hasn't been tested, so use at your own risk.

    // Get the current month number
    $current_month = date('n');
    
    // Show the archives for the current year, by month, 
    // by limiting the returned archives to the current month number
    $args = array(
        'limit' => $current_month
    );
    wp_get_archives( $args );
    
    
    // Awesome, now let's get the previous years, but leave off this year
    $yearly_archives = wp_get_archives('type=yearly&echo=0'); 
    // Split the returned archives at this year...
    $old_archives = explode('</li>', $yearly_archives, 2);
    // And only echo list items that don't include this year.
    echo $old_archives[1];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输