2009-08-03 29 views
1

我想創建一個WordPress的存檔頁面模板,這將是這樣的:存檔模板在WordPress的

2009年8月

  • 後4
  • 後3
  • 後2
  • Post 1

July 2009

  • 後2
  • 後1

所以,基本上,我想從博客所有文章,勒令降日期和月份分組。 有人可以爲我提供這個PHP代碼嗎?

謝謝!

PS:WordPress版本2.8.2是

回答

3

這是我創建了一個前陣子的功能。它基本上做你想做的事情,但它不是一個模板。也許你可以適應它。

<?php 
/** 
* Displays a condensed list of the posts grouped by month/year. 
* 
* @param $order The order of the posts. Either 'DESC' or 'ASC', case sensitive. 
* @param $date_prefix Whether to prefix the posts with the month/date. 
* @param $display Whether to display the results or return it as a String. 
*/ 

function condensed_post_list($order='DESC', $date_prefix=true, $display=true){ 
    global $wpdb; 

    if(!in_array($order, array('DESC','ASC'))) $order = 'DESC'; 
    $query = "SELECT ID, post_title, post_date FROM $wpdb->posts ". 
      "WHERE post_type='post' AND post_status = 'publish' ". 
      "ORDER BY post_date $order"; 
    $results = $wpdb->get_results($query); 

    ob_start(); 
    $current_month = ''; 
    foreach($results as $result) { 
     if($current_month != mysql2date('F Y', $result->post_date)) { 
      if($current_month) echo '</ul>'; 

      $current_month = mysql2date('F Y', $result->post_date); 
      echo '<h2>'.$current_month.'</h2>'; 
      echo '<ul>'; 
     } 
     echo '<li>'; 
     echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : ''); 
     echo '<a href="'.get_permalink($result->ID).'">'; 
     echo $result->post_title.'</a></li>'; 
    } 
    if($current_month) echo '</ul>'; 

    if($display) { 
     ob_end_flush(); 
    } else { 
     return ob_get_clean(); 
    } 
} 
?> 
+0

謝謝你, 您的片段幫了我很多,我設法創建歸檔頁面。唯一的區別是不是直接查詢數據庫,而是使用Wordpress的get_posts函數。 以下是結果:http://victorhurdugaci.com/archive/ – 2009-08-03 20:45:12

+0

謝謝!這是一個很好的幫助 - 雖然我用一些原生的wordpress函數代替了你的SQL查詢,但我只需要從一個類別中提取帖子。我用 - $ results = query_posts('post_type = post&post_status = publish&cat = 3')替換了你的SQL。儘管幫助很大!謝謝!! – AshBrad 2012-03-02 00:13:26

0

我使用上述功能,但取代SQL查詢:

$results = query_posts('post_type=post&post_status=publish&cat=3'); 

這讓我使用優秀的功能@ scompt.com寫的,但它限制在一個單一的博客類別。

0

非常感謝您的幫助。這是我用上面的代碼。

function condensed_post_list($order='DESC', $date_prefix=true, $display=true) 
{ 
    if(!in_array($order, array('DESC','ASC'))) $order = 'DESC'; 
    $args = array(
     'numberposts'  => -1, 
     'orderby'   => 'post_date', 
     'post_type'  => 'post', 
     'post_status'  => 'publish'); 
    $results = get_posts($args); 

    ob_start(); 
    $current_month = ''; 
    foreach($results as $result) { 
     if($current_month != mysql2date('F Y', $result->post_date)) { 
      if($current_month) echo '</ul>'; 

      $current_month = mysql2date('F Y', $result->post_date); 
      echo '<h2>'.$current_month.'</h2>'; 
      echo '<ul>'; 
     } 
     echo '<li>'; 
     echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : ''); 
     echo '<a href="'.get_permalink($result->ID).'">'; 
     echo $result->post_title.'</a></li>'; 
    } 
    if($current_month) echo '</ul>'; 
    if($display) { 
     ob_end_flush(); 
    } 
    else { 
     return ob_get_clean(); 
    } 
}