wordpress 免插件纯代码sitemap.xml网站地图制作
wordpress 建站我一直是拒绝插件的。
最近想弄一个API自动推送到百度站点的,由于网站没有弄站点地图,就在网上找了部分资源,只用代码就能制作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://xiaoshao.top/sitemap.xml
用此方法生成的XML地图在提交Good时会出错,经过寻找安装了一个插件。
版权声明:
作者:惊鸿
链接:https://www.xiaoshao.top/59.html
文章版权归作者所有,未经允许请勿转载。
THE END