代码片段:
<?php
$cat_and_art = $wpdb->get_results(“SELECT t.term_id FROM $wpdb->terms t left join $wpdb->term_taxonomy tt on tt.term_id = t.term_id WHERE tt.taxonomy= ‘category’ and tt.parent=’0′ and tt.count > 0″);
foreach ($cat_and_art as $cat_and_art_id) {
$category_id[] = $cat_and_art_id->term_id;
}
//print_r($category_id);
for($c_id = 0; $c_id < strlen($category_id); $c_id++ ){
?>
<li><?php echo(get_category_parents($category_id[$c_id], TRUE, ”)); ?>
<ul>
<?php /* EDIT THE showposts= NUMBER BELOW FOR MORE POSTS IN EACH CAT */
$my_query = new WP_Query(‘cat=’.$category_id[$c_id].’&showposts=-1′);
while ($my_query->have_posts()) :
$my_query->the_post();
if ( $post->ID == $do_not_duplicate ) continue;
update_post_caches($posts);
$do_not_duplicate = $post->ID;
?>
<li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
</li>
<?php
}
?>
新建一个文件 sitemap.php
首先在文件顶部写入:
<?php
/*
Template Name: sitemap
*/
?>
这样在post page 里就会有 “sitemap” 的选项
然后把上面的代码也一同复制进去。保存到当前theme 文件夹里。
然后后台新建一个页面(page) 填写标题,自定义固定连接,内容为空即可,在右边的模板的下拉菜单里选择刚刚新建的模板。保存页面即可。
Comments
123 responses to “自制WordPress Sitemap Page Template”