自从萌生了辞职的想法后,这两天日日在思考辞职之后我该何去何从,要不要重拾网赚呢?因此我也在恶补这个行业的相关资讯,期间读到 Google 发布的《谷歌搜索引擎优化初学者指南》,里头提到了网站地图(Sitemap)的重要性…于是便有了此文。
从 WordPress 5.5 版本开始,WordPress 默认自带了一个简化的 sitemap 功能。这意味着,你不需要额外安装插件来生成网站的 XML Sitemap。WordPress 会自动为你的网站生成一个基本的 sitemap,通常可以通过访问
https://你的域名/sitemap.xml来查看。这个内建的 sitemap 功能会包括你网站上的主要内容,如文章、页面等。它虽然简单,但对于大多数用户来说已经足够用。
如果你需要更多定制化的功能,比如更复杂的优先级设置、更新频率等,或者想要更精细地控制 Sitemap 的内容,你还是可以选择安装像Yoast SEO或Rank Math这样的 SEO 插件,它们提供了更为强大的 sitemap 功能。
——WordPress 官方已自带网站地图
将下列代码保存为 sitemap.php 放在网站根目录,这时候可以试试打开你的 博客主页/sitemap.php 看看是否添加成功。
<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?> 牧羊人(http://www.shephe.com) -->
<url>
<loc><?php echo get_home_url(); ?></loc>
<lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php
/* 文章页面 */
header("Content-type: text/xml");
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<lastmod><?php the_time('c') ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</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 str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</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 get_term_link($term, $term->slug); ?></loc>
<changefreq>weekly</changefreq>
<priority>0.8</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.4</priority>
</url>
<?php } /* 标签循环结束 */ ?>
</urlset>
如果能正常显示 XML 格式网页则表示代码添加无误,这时候只需要将 博客首页/sitemap.php 重定向到 博客首页/sitemap.xml,具体是修改位于根目录下的 .htaccess 添加语句 RewriteRule ^(sitemap)\.xml$ $1.php,一般情况下应该是这样子的:
RewriteRule ^(sitemap)\.xml$ $1.php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
以上是 apache 的写法,nginx 是在配置文件的 server 块中写入下面的一句:
rewrite /sitemap.xml /sitemap.php last;

代码不错,适合折腾~