网上有很多相关的代码,但其实好多并不适用。有的可能是没有全站网站地图,而有的.xml后缀名需要伪静态设置,特别麻烦。于是,空闲之余,结合网上的部分素材,自己搞了一个。全代码贴出来,有需要的朋友可以拿去。
代码如下:
<?php
include ( "wp-config.php" ) ;
require_once (ABSPATH.'./wp-blog-header.php');
$ltime = get_lastpostmodified(GMT);
$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime));
$posts_to_show = 2000;
$str = '<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
$str.="
<url>
<loc>".get_home_url()."</loc>
<lastmod>".$ltime."</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
";
//文章 code by 3inch.cn
$myposts = get_posts( "numberposts=".$posts_to_show );
foreach( $myposts as $post ) {
$str.="<url>\r\n";
$str.="<loc>".urldecode(get_permalink())."</loc>\r\n";
$str.="<lastmod>".str_replace(" ","T",get_page($page->ID)->post_modified)."</lastmod>\r\n";
$str.="<changefreq>always</changefreq>\r\n";
$str.="<priority>0.9</priority>\r\n";
$str.="</url>\r\n";
}
//标签 code by 3inch.cn
$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;
$str.="<url>\r\n";
$str.="<loc>".urldecode($link)."</loc>\r\n";
$str.="<lastmod>".str_replace(" ","T",get_page($page->ID)->post_modified)."</lastmod>\r\n";
$str.="<changefreq>daily</changefreq>\r\n";
$str.="<priority>0.5</priority>\r\n";
$str.="</url>\r\n";
}
//分类 code by 3inch.cn
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$count = count($terms);
if($count > 0){
foreach ($terms as $term) {
$str.="<url>\r\n";
$str.="<loc>".urldecode(get_term_link($term, $term->slug))."</loc>\r\n";
$str.="<lastmod>".str_replace(" ","T",get_page($page->ID)->post_modified)."</lastmod>\r\n";
$str.="<changefreq>weekly</changefreq>\r\n";
$str.="<priority>0.3</priority>\r\n";
$str.="</url>\r\n";
}
}
//页面 code by 3inch.cn
$mypages = get_pages();
if(count($mypages) > 0) {
foreach($mypages as $page) {
$str.="<url>\r\n";
$str.="<loc>".urldecode(get_page_link($page->ID))."</loc>\r\n";
$str.="<lastmod>".str_replace(" ","T",get_page($page->ID)->post_modified)."</lastmod>\r\n";
$str.="<changefreq>monthly</changefreq>\r\n";
$str.="<priority>0.5</priority>\r\n";
$str.="</url>\r\n";
}
}
$str.="</urlset>";
file_put_contents('./sitemap.xml',$str);
echo '更新 sitemap.xml 成功! <a href="/sitemap.xml"> 查看</a>';
?>
使用很简单,你还不懂的话看教程,又看不懂写个评论或者私信我
使用教程:
新建sitemap.php文件,代码复制进去。上传到网站的根目录下,并访问。例如:http://XXX.XX/sitemap.php(顶级域名下)访问之后将会自动更新sitemap.xml文件。这时就可以用来提交各个搜索引擎进行收录了。
© 版权声明
本站网络名称:
小布博客
本站永久网址:
https://umud.top
网站侵权说明:
本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长QQ1346759989删除处理。
1 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
2 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
3 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
1 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
2 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
3 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
THE END
暂无评论内容