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

知识技术 2021-09-12 19:14 1151
转载请务必注明: 来源:《建站管家》https://www.df81.com/news/226.html

把下面代码文件命名为: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)

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

上一篇: 《建站管家》二次开发文档

下一篇: 同一台Nginx服务器,http站点,加https访问,跳转到其他https站点的解决方法

请先登录后,再发表评论 ~ ~
发表评论
发表评论
发表帖子
广告位招租( ¥5 / 天 )
点击咨询
相关帖子

如果要判断文件是否存在,用函数 is_file(),如果要判断目录是否存在,用函数 is_dir()
用宝塔安装TP程序后,网站前台可以访问,后台无法访问,不是程序问题,而是web服务器环境配置问题(PHP配置),出现这种问题,一般是windows主机安装宝塔造成的,linux安装宝塔不会出现此错误
广告位招租( ¥3 / 天 )
点击咨询
广告位招租( ¥2 / 天 )
点击咨询
广告位招租( ¥1 / 天 )
点击咨询
最新帖子

小朋友,你爸爸是谁啊?这很单纯的,就像我们碰到别人会问:你吃过饭了吗?类似;许多人初次相识,经常会问:你是做什么的啊?你做什么,好像决定你收入的高低,你,资产的范围
1、升级框架到ThinkPHP8.0.3【要求PHP8.1+环境,推荐PHP8.1】 2、启用/禁用调试模式等小细节改进
后台点击生成站点地图 一直转圈圈没有反应,
在线客服