WordPress教程 · WPtech

WordPress代码实现网站地图

小编 · 10月11日 · 2019年 ·

本站以前一直用着wordpress地图插件,想来想去,还是自己代码实现吧.

WordPress代码实现网站地图

网站根目录新建文件

创建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="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'); ?>-->
  <url>
      <loc><?php echo get_home_url(); ?></loc>
	  <mobile:mobile type='pc,mobile'/>
      <lastmod><?php echo gmdate('Y-m-d\TH:i:s+00:00', strtotime(get_lastpostmodified('GMT'))); ?></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>
	  <mobile:mobile type="pc,mobile"/>
      <lastmod><?php the_time('c') ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
  </url>
<?php } // end foreach ?>

<?php
header("Content-type: text/xml");
// 页面
$pages = get_pages('numberposts=-1&orderby=post_date&order=DESC');
foreach($pages as $page) { ?>
    <url>
        <loc><?php echo get_page_link($page->ID); ?></loc>
		<mobile:mobile type='pc,mobile'/>
        <lastmod><?php echo str_replace(" ", "T", get_page($page->ID)->post_modified); ?>+00:00</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.6</priority>
    </url>
<?php } // end foreach ?>


<?php
header("Content-type: text/xml");
// 分类
$categorys = get_terms('category', 'orderby=name&hide_empty=0');
foreach ($categorys as $category) { ?>
    <url>
        <loc><?php echo get_term_link($category, $category->slug); ?></loc>
		<mobile:mobile type='pc,mobile'/>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
<?php } // end foreach ?>
<?php
header("Content-type: text/xml");
// 标签
$tags = get_terms('post_tag', 'orderby=name&hide_empty=0');
foreach ($tags as $tag) { ?>
    <url>
        <loc><?php echo get_term_link($tag, $tag->slug); ?></loc>
		<mobile:mobile type='pc,mobile'/>
        <changefreq>monthly</changefreq>
        <priority>0.4</priority>
    </url>
<?php } // end foreach ?>
</urlset>

创建sitemap.php文件,代码如下

<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
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 https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?>-->
<url>
    <loc><?php echo get_home_url(); ?></loc>
    <lastmod><?php echo gmdate('Y-m-d\TH:i:s+00:00', strtotime(get_lastpostmodified('GMT'))); ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
</url>
<?php
// 文章
$posts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
foreach($posts as $post) :
?>
    <url>
        <loc><?php echo get_permalink($post->ID); ?></loc>
        <lastmod><?php echo str_replace(" ", "T", get_post($post->ID)->post_modified); ?>+00:00</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
<?php
endforeach;
// 页面
$pages = get_pages('numberposts=-1&orderby=post_date&order=DESC');
foreach($pages 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
endforeach;
// 分类
$categorys = get_terms('category', 'orderby=name&hide_empty=0');
foreach ($categorys as $category) :
?>
    <url>
        <loc><?php echo get_term_link($category, $category->slug); ?></loc>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
<?php
endforeach;
// 标签
$tags = get_terms('post_tag', 'orderby=name&hide_empty=0');
foreach ($tags as $tag) :
?>
    <url>
        <loc><?php echo get_term_link($tag, $tag->slug); ?></loc>
        <changefreq>monthly</changefreq>
        <priority>0.4</priority>
    </url>
<?php
endforeach;
?>
</urlset>

接下来需要添加伪静态规则。

在站点根目录找到.htaccess文件,根据服务器服务不同添加如下规则

apache下的规则

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

nginx下规则

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

完成以上步骤,就可以卸载掉wordpress站点地图插件了!

0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!