最新消息:欢迎加入小松的QQ群一起讨论一起学习,搜索页面修改成lua+ElasticSearch,使用php这个页面速度相对慢一点,优化之后速度杠杠的,如有问题请加群联系我

php7安装之重写sitemap

wordpress 602浏览 0评论

在之前我的是sitemap的一个插件,用起来非常的不爽,每次都要手动去生成sitemap,以我每天更新文章的频率每天都要点一下,感觉太麻烦了,不点的话百度站长平台里的sitemap就不会更新

百度站长平台sitemap

 

这不影响收录吗,于是想这个重写一个sitemap.php,然后伪静态成sitemap.xml,这种方式之前做电子商务网站的时候就这么搞的,后来可能对百度蜘蛛不太友好想了想换了种方式,用定时脚步定时生成sitemap.xml
但是一直都没有这么做,心里默念凑合用用于是就这么过来了,就在升级到php7之后,发现插件有问题,点击生成sitemap的时候空白页,应该是有函数不支持或者没有启用,那就直接重写个sitemap
下面我把其中的代码分享一下

<?php
/*sitemap 为了不用每次都要生成sitemap.xml 小松博客 http://www.phpsong.com**/
require_once( dirname( __FILE__ ) . '/wp-load.php' );

$lc_blog_url = home_url();
$blogtime = current_time('timestamp', '1');
$lc_blog_time = date("Y-m-d\TH:i:s+00:00",$blogtime);
$befor_day=date("Y-m-d H:i:s",strtotime("-1 day"));

$site_xml='<?xml version="1.0" encoding="UTF-8" ?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';

$site_xml.= "<url><loc>$lc_blog_url</loc><mobile:mobile type=\"pc,mobile\"/><lastmod>$lc_blog_time</lastmod><changefreq>daily</changefreq><priority>1.0</priority></url>";

$sql_mini = "select ID,post_modified,post_date,post_type,post_title FROM $wpdb->posts
	        WHERE post_password = ''
			AND (post_type='post' or post_type='page')
			AND post_status = 'publish'
			ORDER BY post_modified DESC
	       ";
	$recentposts_mini = $wpdb->get_results($sql_mini);
	if($recentposts_mini){
		foreach ($recentposts_mini as $post){
			
			if( $post->post_type == 'page' ){
				$loc = get_page_link($post->ID);
				$loc=sitemap_xml($loc);
				if(!$loc){ continue; }
				if($post->post_modified == '0000-00-00 00:00:00'){ $post_date = $post->post_date; } else { $post_date = $post->post_modified; } 
				$lastmod = date("Y-m-d\TH:i:s+00:00",strtotime($post_date));
				$changefreq = 'weekly';
				$priority = '0.3';
				$xml_contents_page .= "<url>";
				$xml_contents_page .= "<loc>$loc</loc>";
				$xml_contents_page .= '<mobile:mobile type="pc,mobile"/>';
				$xml_contents_page .= "<lastmod>$lastmod</lastmod>";
				$xml_contents_page .= "<changefreq>$changefreq</changefreq>";
				$xml_contents_page .= "<priority>$priority</priority>";
				$xml_contents_page .= "</url>";
				$html_contents .= '<li><a href="'.$loc.'" title="'.$post->post_title.'" target="_blank">'.$post->post_title.'</a></li>';
			}else{
				$loc = get_permalink($post->ID);
				
				$loc = sitemap_xml($loc);
				if(!$loc){ continue; }
				if($post->post_modified == '0000-00-00 00:00:00'){ $post_date = $post->post_date; } else { $post_date = $post->post_modified; } ##$post->post_date_gmt
				$lastmod = date("Y-m-d\TH:i:s+00:00",strtotime($post_date));
				$changefreq = 'monthly';
				$priority = '0.6';
				$xml_contents_post .= "<url>";
				$xml_contents_post .= "<loc>$loc</loc>";
				$xml_contents_post .= '<mobile:mobile type="pc,mobile"/>';
				$xml_contents_post .= "<lastmod>$lastmod</lastmod>";
				$xml_contents_post .= "<changefreq>$changefreq</changefreq>";
				$xml_contents_post .= "<priority>$priority</priority>";
				$xml_contents_post .= "</url>";

				$html_contents .= '<li><a href="'.$loc.'" title="'.$post->post_title.'" target="_blank">'.$post->post_title.'</a></li>';
				
				if($post->post_date>$befor_day || $post->post_modified>$befor_day){
					
					$tags = wp_get_post_tags($post->ID);
					foreach($tags as $this_tag){
						$loc = get_tag_link($this_tag->term_id);
						$loc = sitemap_xml($loc);
						

						if(!$loc){ continue; }
						$lastmod = date("Y-m-d\TH:i:s+00:00",current_time('timestamp', '1'));
						$changefreq = 'Weekly';
						$priority = '0.3';						$xml_contents_post .= "<url>";
						$xml_contents_post .= "<loc>$loc</loc>";
						$xml_contents_post .= '<mobile:mobile type="pc,mobile"/>';
						$xml_contents_post .= "<lastmod>$lastmod</lastmod>";
						$xml_contents_post .= "<changefreq>$changefreq</changefreq>";
						$xml_contents_post .= "<priority>$priority</priority>";
						$xml_contents_post .= "</url>";
						$not_tags[$tags->term_id]=1;
					}
				}
			
			}
		
		}
	}
#栏目
	$category_ids = get_all_category_ids();
	if($category_ids){
		foreach($category_ids as $cat_id) {
			$loc = get_category_link($cat_id);
			$loc = sitemap_xml($loc);
			if(!$loc){ continue; }
			$lastmod = date("Y-m-d\TH:i:s+00:00",current_time('timestamp', '1'));
			$changefreq = 'Weekly';
			$priority = '0.3';
			$xml_contents_cat .= "<url>";
			$xml_contents_cat .= "<loc>$loc</loc>";
			$xml_contents_cat .= '<mobile:mobile type="pc,mobile"/>';
			$xml_contents_cat .= "<lastmod>$lastmod</lastmod>";
			$xml_contents_cat .= "<changefreq>$changefreq</changefreq>";
			$xml_contents_cat .= "<priority>$priority</priority>";
			$xml_contents_cat .= "</url>";
		}
	}
#tag

	$all_the_tags = get_tags(array('orderby'=>'term_id','order'=>'desc'));
	if($all_the_tags){
		foreach($all_the_tags as $this_tag) {
			
			$tag_id = $this_tag->term_id;
			if($not_tags[$tag_id]){
				continue; 
			}
			
			$loc = get_tag_link($tag_id);
			$loc = sitemap_xml($loc);
			if(!$loc){ continue; }
			$lastmod = date("Y-m-d\TH:i:s+00:00",current_time('timestamp', '1'));
			$changefreq = 'Weekly';
			$priority = '0.3';
			$xml_contents_tag .= "<url>";
			$xml_contents_tag .= "<loc>$loc</loc>";
			$xml_contents_tag .= '<mobile:mobile type="pc,mobile"/>';
			$xml_contents_tag .= "<lastmod>$lastmod</lastmod>";
			$xml_contents_tag .= "<changefreq>$changefreq</changefreq>";
			$xml_contents_tag .= "
<priority>$priority</priority>";
			$xml_contents_tag .= "</url>";
		}
	}
$xml_contents = $xml_contents_post.$xml_contents_page.$xml_contents_cat.$xml_contents_tag;
$site_xml= $site_xml.$xml_contents.'</urlset>';
file_put_contents("sitemap.xml",$site_xml);

function sitemap_xml($string) {
	return str_replace ( array ( '&', '"', "'", '<', '>'), array ( '&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;'), $string);
}

把上面的代码保持到根目录下sitemap.php里,然后用定时脚步执行sitemap.php
wordpress又少了个插件,对wordpress的速度可能有所提升何乐而不为

QQ交流群:136351212(满) 455721967

如无特别说明,本站文章皆为原创,若要转载,务必请注明以下原文信息:
转载保留版权:小松博客» php7安装之重写sitemap
本文链接地址:https://www.phpsong.com/1817.html

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
木有头像就木有JJ!点这里按步骤申请Gravatar头像吧!

网友最新评论 (2)

加载中,请稍候...
  1. 好文章 留言支持下博主
    盈泛网2015-12-09 14:51:20回复
    • 感谢来访
      小松2015-12-09 17:13:14回复