现在的位置: 首页 > Web设计> 正文
为WordPress建立Html格式的Sitemap
2012年06月24日 Web设计 评论数 2 ⁄ 被围观 6,101+

熟悉wordpress建站的童鞋都知道sitemap的重要性,尤其是对网站的SEO以及各大重要搜索引擎的索引都有重要的作用,wordpress已经有许多优秀的sitemap生成插件可以使用,不过他们大部分生成的都只是xml格式的数据,可以直接提交到搜索引擎,方便搜索引擎的索引,但是不方便访客的阅读。

本文介绍一下怎样客制化和建立自己的Sitemap Page页面,可以包括或者排除特定的页面,特定的分类,加入自己定制的信息等等。

建立步骤

1. 为了代码的重用,我们需要利用wordpress主题模板的特性(theme partial),在当前的主题文件夹下面新建立一个partials文件夹,然后在里面新建立一个sitemap.php文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<h2 id="authors">Authors</h2>
<ul>
<?php
wp_list_authors(
array(
'exclude_admin' => false,
)
);
?>
</ul>
 
<h2 id="pages">Pages</h2>
<ul>
<?php
// Add pages you'd like to exclude in the exclude here
wp_list_pages(
array(
'exclude' => '',
'title_li' => '',
)
);
?>
</ul>
 
<h2 id="posts">Posts</h2>
<ul>
<?php
 
// Add categories you'd like to exclude in the exclude here
$cats = get_categories('exclude=');
foreach ($cats as $cat) {
echo "<li><h3>".$cat->cat_name."</h3>";
echo "<ul>";
query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
while(have_posts()) {
the_post();
$category = get_the_category();
// Only display a post link once, even if it's in multiple categories
if ($category[0]->cat_ID == $cat->cat_ID) {
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}
}
echo "</ul>";
echo "</li>";
}
 
//Add Custom Post Types to your HTML Sitemap
foreach( get_post_types( array('public' => true) ) as $post_type ) {
if ( in_array( $post_type, array('post','page','attachment') ) )
continue;
 
$pt = get_post_type_object( $post_type );
 
echo '<h2>'.$pt->labels->name.'</h2>';
echo '<ul>';
 
query_posts('post_type='.$post_type.'&posts_per_page=-1');
while( have_posts() ) {
the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}
 
echo '</ul>';
}
?>
</ul>
<h2 id="authors">Authors</h2>
<ul>
<?php
wp_list_authors(
array(
'exclude_admin' => false,
)
);
?>
</ul>

<h2 id="pages">Pages</h2>
<ul>
<?php
// Add pages you'd like to exclude in the exclude here
wp_list_pages(
array(
'exclude' => '',
'title_li' => '',
)
);
?>
</ul>

<h2 id="posts">Posts</h2>
<ul>
<?php

// Add categories you'd like to exclude in the exclude here
$cats = get_categories('exclude=');
foreach ($cats as $cat) {
echo "<li><h3>".$cat->cat_name."</h3>";
echo "<ul>";
query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
while(have_posts()) {
the_post();
$category = get_the_category();
// Only display a post link once, even if it's in multiple categories
if ($category[0]->cat_ID == $cat->cat_ID) {
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}
}
echo "</ul>";
echo "</li>";
}

//Add Custom Post Types to your HTML Sitemap
foreach( get_post_types( array('public' => true) ) as $post_type ) {
if ( in_array( $post_type, array('post','page','attachment') ) )
continue;

$pt = get_post_type_object( $post_type );

echo '<h2>'.$pt->labels->name.'</h2>';
echo '<ul>';

query_posts('post_type='.$post_type.'&posts_per_page=-1');
while( have_posts() ) {
the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}

echo '</ul>';
}
?>
</ul>

2. 拷贝一份当前主题下面的page.php,并重新命名为page-sitemap.php,编辑内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
/*
Template Name: Sitemap Page
*/
?>
<?php get_header(); ?>
<div id="content">
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<!-- menu -->
<div id="map">
<div class="browse">现在位置 ><a title="返回首页" href="<?php echo get_settings('Home'); ?>/">首页</a><?php the_title(); ?></div>
<div id="feed"><a href="<?php echo get_option('swt_rsssub'); ?>" title="RSS">RSS</a></div>
</div>
<!-- end: menu -->
<!-- entry -->
<div class="clear"></div>
<div class="entry_box_s">
<div class="entry">
<div class="page" id="post-<?php the_ID(); ?>">
<?php the_content('More &raquo;'); ?>
<div class="clear"></div>
</div>
<?php get_template_part('/partials/sitemap'); ?>
</div>
<!-- end: entry -->
<div class="clear"></div>
<i class="lt"></i>
<i class="rt"></i>
</div>
<div class="entry_sb">
<i class="lb"></i>
<i class="rb"></i>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<!-- end: content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php
/*
Template Name: Sitemap Page
*/
?>
<?php get_header(); ?>
<div id="content">
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<!-- menu -->
<div id="map">
<div class="browse">现在位置 ><a title="返回首页" href="<?php echo get_settings('Home'); ?>/">首页</a> ><?php the_title(); ?></div>
<div id="feed"><a href="<?php echo get_option('swt_rsssub'); ?>" title="RSS">RSS</a></div>
</div>
<!-- end: menu -->
<!-- entry -->
<div class="clear"></div>
<div class="entry_box_s">
<div class="entry">
<div class="page" id="post-<?php the_ID(); ?>">
<?php the_content('More &raquo;'); ?>
<div class="clear"></div>
</div>
<?php get_template_part('/partials/sitemap'); ?>
</div>
<!-- end: entry -->
<div class="clear"></div>
<i class="lt"></i>
<i class="rt"></i>
</div>
<div class="entry_sb">
<i class="lb"></i>
<i class="rb"></i>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<!-- end: content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

主要修改两个地方,一个是在文件的顶部加入

 <?php
/*
Template Name: Sitemap Page
*/
?>

另一个是调用sitemap.php,加入

 <?php get_template_part('/partials/sitemap'); ?>

3. 新建立一个页面,模板选择“Sitemap Page”,示例 http://blog.zhourunsheng.com/sitemap/

目前有 2 条留言 其中:访客:4 条, 博主:0 条

  1. 隔壁大叔 : 2013年06月20日10:07:58  -49楼 @回复 回复

    想问下博主,那个sitemap文件直接建立在当前使用的主题文件夹下可行否?

  2. 隔壁大叔 : 2013年06月20日10:01:15  -48楼 @回复 回复

    现在用的是别人写的一个插件,看来可以换了,谢谢分享了O(∩_∩)O~~

  3. Adeploy : 2012年08月01日13:05:06  -47楼 @回复 回复

    你这个代码尚亮实在太漂亮了

  4. P57减肥药 : 2012年06月24日17:59:30  -46楼 @回复 回复

    博主的文章不错

给我留言

留言无头像?


×
腾讯微博