wordpress - How can I display post link group by y

2020-08-01 08:32发布

I want to display a list of post link group by year. Archive function don't do it Eg:

2010
  Blender 2.53 released
  Gamequery 0.5 released
  synfig 0.62.02 released
  ...

2009
  Gimp 2.67 released
  Blender 2.52 released

How can I do it?

标签: wordpress
3条回答
趁早两清
2楼-- · 2020-08-01 09:02

I think you'll have to make your own custom query to get the posts by year. Something like this:

$query = $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' group by postyear", $wpdb->siteid );

Note: where I put "group by postyear", I'm not sure what that column name would be, so you'd have to figure out how that's stored in your blogs table. That may be a case where you have to parse out the year from a date field. Give that a shot. Then you can format your output like this:

$terms = $wpdb->get_results($query,ARRAY_A);
    echo "<ul>";    
    foreach($terms as $detail)
    {
        echo "<li style='font-size:1.3em;'><a href='http://".$detail[ 'domain' ].$detail['path']."'>".get_blog_option( $detail['blog_id'], 'blogname' )."</a></li>";
    }
    echo "</ul>";
查看更多
可以哭但决不认输i
3楼-- · 2020-08-01 09:16

Query all your posts, ordered by post date (descending I assume?). You print them one by one, remember the year, and when the year of the current post is not the same as the year of the previous post, you know you have to print a year label.

query_posts(array('nopaging' => 1, /* we want all posts, so disable paging. Order by date is default */));
$prev_year = null;
if ( have_posts() ) {
   while ( have_posts() ) {
      the_post();
      $this_year = get_the_date('Y');
      if ($prev_year != $this_year) {
          // Year boundary
          if (!is_null($prev_year)) {
             // A list is already open, close it first
             echo '</ul>';
          }
          echo '<h3>' . $this_year . '</h3>';
          echo '<ul>';
      }
      echo '<li>';
      // Print the link to your post here, left as an exercise to the reader
      echo '</li>';
      $prev_year = $this_year;
   }
   echo '</ul>';
}
查看更多
We Are One
4楼-- · 2020-08-01 09:17

You may want to try the following code:

wp_get_archives(array('type' => 'yearly'));

There are several more parameters to try. Please refer to http://codex.wordpress.org/Function_Reference/wp_get_archives for details.

查看更多
登录 后发表回答