把下面代码文件命名为:sitemap.php,然后做伪静态为:sitemap.xml,然后在宝塔里设置定期刷新sitemap即可:
<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 10000;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">';
/* 首页 */
?>
<url>
<loc><?php echo get_home_url(); ?></loc>
<lastmod><?php $ltime = get_lastpostmodified("GMT");$ltime = gmdate('Y-m-d', strtotime($ltime)); echo $ltime; ?></lastmod>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>
<?php
/* 文章页 */
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<lastmod><?php the_time('Y-m-d') ?></lastmod>
<changefreq>daily</changefreq>
<priority>0.9</priority>
</url>
<?php } ?>
<?php
/* 分类(目录)页 */
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$count = count($terms);
if($count > 0){
foreach ($terms as $term) {
?>
<url>
<loc><?php echo str_replace('/./','/',get_term_link($term, $term->slug)); ?></loc>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<?php
}
}
?>
<?php
/* 单页面 */
$mypages = get_pages();
if(count($mypages) > 0) {
foreach($mypages as $page) {
?>
<url>
<loc><?php echo get_page_link($page->ID); ?></loc>
<lastmod><?php echo date('Y-m-d',strtotime(get_page($page->ID)->post_modified)); ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.7</priority>
</url>
<?php
}
}
?>
<?php
/* 标签页 */
$tags = get_terms("post_tag");
foreach ( $tags as $key => $tag ) {
$link = get_term_link( intval($tag->term_id), "post_tag" );
if ( is_wp_error( $link ) )
return false;
$tags[ $key ]->link = $link;
?>
<url>
<loc><?php echo $link; ?></loc>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php
}
?>
<?php
/* 搜索页 */
foreach ($tags as $tag){
?>
<url>
<loc><?php echo get_home_url().'/search/'.str_replace('-','+',$tag->slug).'/'; ?></loc>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
<?php
}
?>
</urlset>上一篇: wordpress禁用gravatar并用默认头像替换等小问题处理
下一篇: 今夜难眠