wordpress自动生成sitemap网站地图的代码,无须插件

南国老符 南国老符 最后更新于:2021-09-12 浏览:1405

把下面代码文件命名为:sitemap.php,然后做伪静态为:sitemap.xml,然后在宝塔里设置定期刷新sitemap即可:


<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 10000;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">';
/* 首页 */
?>
<url>
    <loc><?php echo get_home_url(); ?></loc>
    <lastmod><?php $ltime = get_lastpostmodified("GMT");$ltime = gmdate('Y-m-d', strtotime($ltime)); echo $ltime; ?></lastmod>
    <changefreq>always</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('Y-m-d') ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</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 str_replace('/./','/',get_term_link($term, $term->slug)); ?></loc>
    <changefreq>daily</changefreq>
    <priority>0.8</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 date('Y-m-d',strtotime(get_page($page->ID)->post_modified)); ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.7</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.6</priority>
</url>
<?php
}
?>
<?php
/* 搜索页 */
foreach ($tags as $tag){
?>
<url>
    <loc><?php echo get_home_url().'/search/'.str_replace('-','+',$tag->slug).'/'; ?></loc>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
</url>
<?php
}
?>
</urlset>



点赞 (0)

收藏 (0)
转载请务必注明: 来源:《建站管家》https://www.df81.com/news/226.html

本站部分信息来源网络,如有侵权,请联系QQ:1062129401删除。

上一篇: wordpress禁用gravatar并用默认头像替换等小问题处理

下一篇: 今夜难眠

请先登录后,再发表评论 ~ ~
发表评论
评论 (0)

发表帖子
广告位招租( ¥5 / 天 )
点击咨询
最新帖子

南国老符
2025-05-26 16:54
南国老符
2024-09-29 10:29
南国老符
2023-12-15 21:41
广告位招租( ¥5 / 天 )
点击咨询
最近评论

U
user1379
回复:版本号多少?
U
user1379
回复:新版本修复了,下载新版本即可
U
user1379
U
user1304
回复:默认没有这个功能,你可以自行二次开发后期可能会考虑在站群版本上加入此功能及商城功能
Y
yuanis90
回复:后台->语言->全局管理->模型管理->编辑->启用关键词内链即可
Y
yuanis90
回复:后台已经启用了,但是前台产品说明的关分键词还是没有自动链接到,
购买VIP
购买授权
余额充值
发表帖子
客服微信