1、在wordpress网站目录里添加一个sitemap.php文件,代码如下:
<?php /** * 自动生成网站地图sitemap.xml文件 * 接口链接:https://域名/sitemap.php */ error_reporting(E_ALL ^ E_DEPRECATED); header("Content-type: text/html; charset=utf-8"); include ( "wp-config.php" ) ; $lastmod = date('Y-m-d\TH:i:s+00:00'); $str = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; $str.=" <url> <loc>".get_home_url()."</loc> <lastmod>".$lastmod."</lastmod> <changefreq>weekly</changefreq> <priority>1.0</priority> </url> "; //获取全部已发布文章 $args = array( 'posts_per_page' => -1, 'post_type' => 'post', 'post_status' => 'publish'//已发布 ); $postsList = get_posts($args); foreach( $postsList 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"; } //标签 $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"; } //分类 $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"; } } //页面 $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>'; ?>
2、宝塔面板中 → 计划任务 → 添加如下任务:
任务类型:访问URL URL地址:http://您的网站域名/sitemap.php 执行周期:设置为每天0小时30分执行
请登录之后再进行评论