释放双眼,带上耳机,听听看~!
获取所有链接
将下方代码复制到utf8的文件里,分别命名为post.php(文章)、page.php(页面)、category.php(分类)、tag.php(标签),放置在网站根目录即可。
获取所有文章链接代码↓
<?php require('./wp-blog-header.php'); header("Content-type: text/xml"); header('HTTP/1.1 200 OK'); $posts_to_show = 2000; //提示:这里的数字决定获取多少个文章链接,可根据自己站点服务器实际情况来具体填写,由于网站只有1000+文章,所以设置2000 echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">' ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 缙哥哥的博客(http://www.dujin.org)--> <?php $myposts = get_posts( "numberposts=" . $posts_to_show ); foreach( $myposts as $post ) { ?> <url> <loc><?php the_permalink(); ?></loc> </url> <?php }?> </urlset>
获取所有页面链接代码↓
<?php require('./wp-blog-header.php'); header("Content-type: text/xml"); header('HTTP/1.1 200 OK'); $posts_to_show = 100; //提示:页面一般不会很多,可根据自己的实际情况修改。 echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">' ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 缙哥哥的博客(http://www.dujin.org)--> <?php $mypages = get_pages(); if(count($mypages) > 0) { foreach($mypages as $page) { ?> <url> <loc><?php echo get_page_link($page->ID); ?></loc> </url> <?php }?> </urlset>
获取所有分类链接代码↓
<?php require('./wp-blog-header.php'); header("Content-type: text/xml"); header('HTTP/1.1 200 OK'); $posts_to_show = 500; //提示:自己根据需求修改 echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">' ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 缙哥哥的博客(http://www.dujin.org)--> <?php /* 博客分类 */ $terms = get_terms('category', 'orderby=name&hide_empty=0' ); $count = count($terms); if($count > 0){ foreach ($terms as $term) { ?> <url> <loc><?php echo get_term_link($term, $term->slug); ?></loc> </url> <?php }?> </urlset>
获取所有标签链接代码↓
<?php require('./wp-blog-header.php'); header("Content-type: text/xml"); header('HTTP/1.1 200 OK'); $posts_to_show = 2000; //提示:这里的数字决定获取多少个文章链接,可根据自己站点服务器实际情况来具体填写,由于网站只有1000+文章,所以设置2000 echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">' ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 缙哥哥的博客(http://www.dujin.org)--> <?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> </url> <?php }?> </urlset>