這是一個國外朋友需要的蛋疼需求,其實這跟網站地圖差不多,只是這個 「輸出所有文章列表」 更簡單 + 用表格 「打印」 出來而已。不需要考慮分類,只要按照日期倒序排列出來,另外加上倒序序號。
既然有需求就寫出來吧,或許看懂中文的朋友也有這類需求,因為通常國人更蛋疼。
因為要倒序序號排列,所以這裏需要先統計出所有文章 (已發表並公開) 的文章數量,然後遞減輸出。
我記得以前統計文章總數用的是 SQL 語句實現的,其實我們平時要儘量少用 SQL,除非 WP 官方木有提供封裝好的函數。 WP 2.5 開始,WP 就提供了專門統計文章數量的函數:wp_count_posts(),具體用法圍觀 WordPress Codex : 傳送門
下面説説具體實現方法和代碼
1. Copy 一份所用主題的 page.php,改名為 page-allpost.php
2. 打開 page-allpost.php,找到類似下面這個文章內容輸出函數
在其下面添加如下代碼並保存
| <div id="page-allpost">
<table> <strong>All Post</strong> <tr> <td><strong>S.No</strong></td> <td><strong>Published Date</strong></td> <td><strong>Post Header</strong></td> </tr> <?php $count_posts = wp_count_posts(); $published_posts = $count_posts->publish; query_posts('posts_per_page=-1' ); while ( have_posts() ) : the_post(); echo '<tr>'; echo '<td>'.$published_posts.'</td>'; echo '<td width="120">'; the_time(get_option( 'date_format' )); echo '</td><td><a href="http://'/span; spanthe_permalink/span();/span/ppspanspanecho/span span'" title="'.esc_attr( get_the_title() ).'">'; the_title(); echo '</a></td></tr>'; $published_posts--; endwhile; wp_reset_query(); ?> </table> </div> |
3. 新建一個頁面,別名 (slug) 為 allpost
4. 木了,搞定了。 (PS:可以用 page-allpost 這個 id 定義表格樣式)
竟然忘了上傳效果圖:重上如下
