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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)