这个功能是小白兔帮我做的,是利用字串符把文章、分类、标签、页面的一一循环出来,这样就会得到一个完整的网站地图功能,然后生成页面,生成位置一般都是在网站根目录下面的,下面开始贴代码。
首先加入头部的
<?php require(ABSPATH.'/wp-blog-header.php'); //加载文件 $posts_to_show = 10000; //这里限制数量 $XMLheader = '<?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"> '; $XMLlastpostdate = get_lastpostdate('blog'); global $sitemap_str; $sitemap_str = $XMLheader; $sitemap_str.= '<!--last post:'.$XMLlastpostdate.'-->'; //时间日期
接下来是首页的。
$sitemap_str.= ' <url> <loc>'.get_home_url().'</loc> <!--链接--> <lastmod>'.gmdate('Y-m-d').'</lastmod> <!--时间--> <changefreq>always</changefreq> <!--更新频率--> <priority>1.0</priority> <!--权重--> </url> ';
循环文章页的所有链接
$myposts = get_posts( "numberposts=" . $posts_to_show ); foreach( $myposts as $post ) { $sitemap_str.=' <url> <loc>'.get_the_permalink($post->ID).'</loc> <lastmod>'.get_the_time('Y-m-d G:i:s',$post->ID).'</lastmod> <changefreq>always</changefreq> <priority>0.6</priority> </url> '; }
循环所有页面的地图
$mypages = get_pages(); if(count($mypages) > 0) { foreach($mypages as $page) { $pagetime = get_page($page->ID)->post_modified; $sitemap_str.=' <url> <loc>'.get_page_link($page->ID).'</loc> <lastmod>'.$pagetime.'</lastmod> <changefreq>always</changefreq> <priority>0.6</priority> </url> '; } }
循环所有分类的地图
$terms = get_terms('category', 'orderby=name&hide_empty=0' ); $count = count($terms); if($count > 0){ foreach ($terms as $term) { $sitemap_str.=' <url> <loc>'.get_term_link($term, $term->slug).'</loc> <changefreq>always</changefreq> <priority>0.8</priority> </url> '; } }
循环所有tag的地图
$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; $sitemap_str.=' <url> <loc>'.$link.'</loc> <lastmod>'.get_the_modified_time('Y-n-d H:i:s').'</lastmod> <changefreq>always</changefreq> <priority>0.4</priority> </url> '; }
最后一个是在循环执行一个单独的查询之后,此函数将$post global恢复到主查询中的当前post。
wp_reset_postdata();
剩下的就是网站地图生成的页面格式和生成位置,并且给个钩子
function set_sitemap(){ require_once('sitemap-xml.php'); //这个需要注意路径 global $sitemap_str; file_put_contents(ABSPATH.'/sitemap.xml',$sitemap_str); //这里是生成的文件名字 } add_action("init", "set_sitemap"); //这里是钩子
看完零散的,下面贴出完整的网站地图代码
<?php require(ABSPATH.'/wp-blog-header.php'); //加载文件 $posts_to_show = 10000; //这里限制数量 $XMLheader = '<?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"> '; $XMLlastpostdate = get_lastpostdate('blog'); global $sitemap_str; $sitemap_str = $XMLheader; $sitemap_str.= '<!--last post:'.$XMLlastpostdate.'-->'; //时间日期 $sitemap_str.= ' <url> <loc>'.get_home_url().'</loc> <!--链接--> <lastmod>'.gmdate('Y-m-d').'</lastmod> <!--时间--> <changefreq>always</changefreq> <!--更新频率--> <priority>1.0</priority> <!--权重--> </url> '; $myposts = get_posts( "numberposts=" . $posts_to_show ); foreach( $myposts as $post ) { $sitemap_str.=' <url> <loc>'.get_the_permalink($post->ID).'</loc> <lastmod>'.get_the_time('Y-m-d G:i:s',$post->ID).'</lastmod> <changefreq>always</changefreq> <priority>0.6</priority> </url> '; } $mypages = get_pages(); if(count($mypages) > 0) { foreach($mypages as $page) { $pagetime = get_page($page->ID)->post_modified; $sitemap_str.=' <url> <loc>'.get_page_link($page->ID).'</loc> <lastmod>'.$pagetime.'</lastmod> <changefreq>always</changefreq> <priority>0.6</priority> </url> '; } } $terms = get_terms('category', 'orderby=name&hide_empty=0' ); $count = count($terms); if($count > 0){ foreach ($terms as $term) { $sitemap_str.=' <url> <loc>'.get_term_link($term, $term->slug).'</loc> <changefreq>always</changefreq> <priority>0.8</priority> </url> '; } } $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; $sitemap_str.=' <url> <loc>'.$link.'</loc> <lastmod>'.get_the_modified_time('Y-n-d H:i:s').'</lastmod> <changefreq>always</changefreq> <priority>0.4</priority> </url> '; } wp_reset_postdata(); $sitemap_str.='</urlset>'; function set_sitemap(){ require_once('sitemap-xml.php'); global $sitemap_str; file_put_contents(ABSPATH.'/sitemap.xml',$sitemap_str); } add_action("init", "set_sitemap"); ?>