WordPress代码实现xml网站地图

WordPress代码实现xml网站地图

作者 : 资源客 发布时间: 2019-09-6

WordPress不使用插件代码实现博客网站地图sitemap.xml的方法,需要添加伪静态规则。

在网站根目录新建一个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>

伪静态

rewrite ^/Sitemap.xml$ /

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » WordPress代码实现xml网站地图