2010-08-03 55 views

回答

1

我想你必須做出自己的自定義查詢來獲取每年的帖子。事情是這樣的:

$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); 

注:這裏我把「通過postyear組」,我不知道那是什麼列名會是這樣,所以你必須弄清楚,如何存儲在您的博客表。這可能是您需要從日期欄中解析出年份的情況。給那一槍。然後,你可以格式化你的輸出是這樣的:

$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>"; 
2

你可能要嘗試下面的代碼:

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

有幾個參數試試。詳情請參閱http://codex.wordpress.org/Function_Reference/wp_get_archives

+0

第二個答案:它生成一個檔案'列表鏈接到頁面,而不是鏈接到帖子! – Henrique 2010-08-04 19:47:29

+2

請冷靜下來。我確定wp_get_archives函數生​​成指向帖子的鏈接。請嘗試「wp_get_archives(array('type'=>'postbypost'));」 – Box 2010-08-05 00:05:27

+0

好吧,但年份和類別呢? – Henrique 2010-09-02 18:48:30

8

查詢您的所有帖子,按發佈日期排序(降序,我假設?)。您可以逐一打印它們,記住年份,以及當前帖子的年份與上一篇帖子的年份不同時,您知道必須打印年份標籤。

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>'; 
} 
+0

我想添加get_year_link($ this_year),如果他們想要鏈接到一個存檔,可以對他人有用。 – Voles 2012-03-30 02:39:14

+0

}>

  • <代替//打印鏈接到您的帖子在這裏,作爲練習留給讀者 – 2012-07-22 16:27:16

    +0

    使用'回聲'

  • ' . get_the_title() .'
  • 'PHP ;?',而不是'回聲'
  • 「; //將鏈接打印到您的文章中,作爲練習留給讀者 echo'
  • ';'如果您正確寫入其他行的地址 – 2012-11-07 20:23:15