GG资源网

WordPress的主题免插件自动更新的sitemap.xml站点地图(纯代码实现WordPress自动生成sitemap.xml网站地图功能)

WordPress的主题免插件自动更新的sitemap.xml站点地图

不喜欢给WordPress的主题安装各种插件的话,可以用下面纯代码的方法生成网站地图站点地图,可以同时生成首页,文章,单页面,状语从句:分类照片标签的网站地图

<?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’); ?>(https://www.tinggezhao.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

/* 文章页面 */

$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>

将以上代码保存为sitemap.php放在网站根目录,然后复制以下代码:

RewriteRule ^sitemap.xml$ sitemap.php [L]

加入到伪静态规则.htaccess文件里的RewriteBase /的下面,保存即可。

然后通过https://www.tinggezhao.com/sitemap.xml来访问站点地图,这个地址就可以提交到各大搜索引擎。

注意:有些同学按照网上的教程弄了,但是不会自动更新,那是因为[ L ]这个符号没有复制粘贴上去。

纯代码实现WordPress自动生成sitemap.xml网站地图功能

sitemap.xml也就是网址地图,这对网站的优化是很有好处的,站点地图这玩意真的很有必要整一个。

很多站长都是直接用插件生成一个sitemap.xml,没毛病,也是可以的,但是wp自学笔记个人觉得能用代码实现的,就不会用插件,下面就分享纯代码实现WordPress自动生成sitemap.xml网站地图功能:

新建文件

首先新建一个“sitemap.php”文件,然后将下面代码复制到sitemap.php文件中。

将编辑好的sitemap.php文件,放在网站的根目录。

设置伪静态规则

不同的环境不同,也存在操作方面的问题,wp自学笔记使用的是宝塔面板,操作相对来说比较简单。

只需要进入宝塔的后台,然后找到你建立的网站,在设置—伪静态,选择wordpress,重启Nginx服务器即可。

首先将自己所使用的的sitemap.xml插件都禁止,然后看看网站的根目录是否会生成一个sitemap.xml文件,如果已经有了,你可以试一下你的域名/sitemap.xml,如果可以显示网站地图信息,那么就是没有问题了。

恭喜你已经成功通过自己的努力,建立了一个没有任何第三方外链的网站地图sitemap.xml

由于网站搬家,部分链接失效,如无法下载,请联系站长!谢谢支持!
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » WordPress的主题免插件自动更新的sitemap.xml站点地图(纯代码实现WordPress自动生成sitemap.xml网站地图功能)

发表回复

CAPTCHAis initialing...