【问题标题】:Wordpress - archive page: list posts by yearWordpress - 存档页面:按年份列出帖子
【发布时间】:2014-02-16 17:45:30
【问题描述】:

我需要这样的东西:

2014

  • 发布 1
  • 发布 2
  • 发布 3
  • 发布 4

2013

  • 发布 1
  • 发布 2
  • 发布 3

我怎样才能做到这一点?

【问题讨论】:

  • 我不确定 Wordpress 是如何存储发布日期的,但是在查询时你需要做这样的事情。 SELECT * FROM posts ORDER BY date

标签: php wordpress archive


【解决方案1】:

这是你需要的:

$yearly = new WP_Query(array('posts_per_page' => -1));
$prev_year = null;
if( $yearly->have_posts() ) : while( $yearly->have_posts() ) : $yearly->the_post();
      $this_year = get_the_date('Y');
      if ($prev_year != $this_year) {
          if (!is_null($prev_year)) {
             echo '</ul>';
          }
          echo '<h3>' . $this_year . '</h3>';
          echo '<ul>';
      }
      echo '<li>';
      echo '</li>';
      $prev_year = $this_year;
    endwhile;    
    echo '</ul>';
endif;

请同时查看文档:http://codex.wordpress.org/Class_Reference/WP_Query

【讨论】:

    猜你喜欢
    • 2014-08-03
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多