建立一個 HTML 的網站地圖 (sitemap) 對於 SEO 和用户體驗都有一定的優化,而單純的建立一個 xml 的網站地圖則是有利於 SEO 優化,之前小編曾經發布過一個 WordPress 的 xml sitemap 的插件:google sitemap generator,那麼今天小編就來介紹下如何使用非插件的方法建立 HTML 以及 XML 格式的網站地圖。

WordPresss 非插件建立 html 格式的網站地圖的方法:

1 、首先下載小編這裏為大家準備好了的 html 站點地圖的模板文件:百度網盤

2 、將 html 站點地圖模板文件上傳至當前使用的 WordPress 主題的目錄下。

3 、在後台新建一個內容為空標題為站點地圖,模板為站點地圖地圖的頁面,操作圖如下:

選擇模板

然後發佈即可創建 html 格式的站點地圖,好了,説完了如何不用插件製作 WordPress 的 html 格式的網站地圖,接下來小編在教大家如何不使用插件創建 WordPress 的 xml 格式的網站地圖。

xml 地圖的創建方法就更簡單了,新建一個名為 xmlmap.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?>-->
  <url>
      <loc>https://www.weixiaoduo.com/</loc>
      <lastmod><?php echo get_lastpostdate('blog'); ?></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 } // end foreach ?>
</urlset>

將 xmlmap.php 文件傳至網站根目錄,然後根據環境寫好 url 轉發規則。

首先是 apache 下的規則:

RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ xmlmap.php

將以上代碼加入到.htaccess 文件即可,接下來是 nginx 下規則:

rewrite ^/sitemap.xml$ /xmlmap.php;

至於微軟的 iis,小編沒用過,規則怎麼寫?自己慢慢琢磨吧。