WordPress 用自定义页面输出所有文章列表(网站地图)

WordPress 如何输出所有文章列表?其实就是网站地图功能,只不过有人需要输出所有文章列表,并且以表格模式的需求,下面教大家如何实现:

1. 复制一份所用主题的 page.php,改名为 page-allpost.php

the_content(); ?>

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="'; the_permalink();

echo '" 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 定义表格样式),如果如下图

# 更多WordPress技巧,请关注「WordPress专题