我们知道网站地址有利于搜索引擎的收录,wordpress有自带的xml格式的网站地图,如果我们也希望生成txt格式的地图呢?本文我们用纯代码方法来实现.
该方法不需要安装任何插件,纯代码生成.
- <?php
- require('./wp-blog-header.php');
- header('Content-type: application/txt');
- header('HTTP/1.1 200 OK');
- $posts_to_show = 50000; // 限制最大文章数量
- ?>
- <?php
- header("Content-type: text/txt");
- $myposts = get_posts( "numberposts=" . $posts_to_show );
- <a href="/tags.php/foreach/" target="_blank">foreach</a>( $myposts as $post ) {
- //Vevb.com
- ?>
- <?php the_permalink(); ?><?php echo "/n"; ?>
- <?php } ?>
将上述代码复制保存为php文件,注意使用utf-8格式,然后将其上传到你的wordpress安装根目录上.
注意:将www.admin.com改为你的网站地址。
设置伪静态
①、Nginx
编辑已存在的Nginx伪静态规则,新增如下规则后(平滑)重启nginx即可:
rewrite ^/ping.txt$ /ping.php last;
②、Apache
编辑网站根目录的 .htaccess,加入如下规则:
RewriteRule ^(ping)/.xml$ $1.php
做好伪静态规则后,就可以直接访问sitemap.xml看看效果了.
最后我们输入http://www.admin.com/ping.txt就可以看到wordpress无插件纯代码生成txt格式网站地图的效果了,如果需要下载该txt文件,只需要右键另存为即可.
WordPress免插件生成完整站点地图(sitemap.xml)的php代码:
- <?php
- require('./wp-blog-header.php');
- header("Content-type: text/xml");
- header('HTTP/1.1 200 OK');
- $posts_to_show = 1000;
- echo '<?xml version="1.0" encoding="UTF-8"?>';
- echo '<urlset xmlns="http://www.sitema<a href="/fw/photo.html" target="_blank">ps</a>.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
- ?>
- <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>-->
- <url>
- <loc><?php echo get_home_url(); ?></loc>
- <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d/TH:i:s+00:00', <a href="/tags.php/strtotime/" target="_blank">strtotime</a>($ltime)); echo $ltime; ?></lastmod>
- <changefreq>daily</changefreq>
- <priority>1.0</priority>
- </url>
- <?php
- /* 文章页面 */
- header("Content-type: text/xml");
- $myposts = get_posts( "numberposts=" . $posts_to_show );
- foreach( $myposts as $post ) { ?>
- <url>
- <loc><?php the_permalink(); ?></loc>
- <lastmod><?php the_time('c') ?></lastmod>
- <changefreq>monthly</changefreq>
- <priority>0.6</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 str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>
- <changefreq>weekly</changefreq>
- <priority>0.6</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 get_term_link($term, $term->slug); ?></loc>
- <changefreq>weekly</changefreq>
- <priority>0.8</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.4</priority>
- </url>
- <?php } /* 标签循环结束 */ ?>
- </urlset>
wordpress非插件实现xml格式网站地图.
- <?php
- require('./wp-blog-header.php');
- header("Content-type: text/xml");
- header('HTTP/1.1 200 OK');
- $posts_to_show = 1000; // 获取文章数量
- echo '<?xml version="1.0" encoding="UTF-8"?>';
- echo '<urlset xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" rel="external nofollow" >http://www.w3.org/2001/XMLSchema-instance</a>" xmlns="<a href="http://www.sitemaps.org/schemas/sitemap/0.9" rel="external nofollow" rel="external nofollow" >http://www.sitemaps.org/schemas/sitemap/0.9</a>"
- xsi:schemaLocation="<a href="http://www.sitemaps.org/schemas/sitemap/0.9" rel="external nofollow" rel="external nofollow" >http://www.sitemaps.org/schemas/sitemap/0.9</a> <a href="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'" rel="external nofollow" >http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'</a>;
- ?>
- <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>-->
- <url>
- <loc>http://localhost/</loc>
- <lastmod><?php echo get_lastpostdate('blog'); ?></lastmod>
- <changefreq>daily</changefreq>
- <priority>1.0</priority>
- </url>
- <?php
- header("Content-type: text/xml");
- $myposts = get_posts( "numberposts=" . $posts_to_show );
- foreach( $myposts as $post ) { ?>
- <url>
- <loc><?php the_permalink(); ?></loc>
- <lastmod><?php the_time('c') ?></lastmod>
- <changefreq>monthly</changefreq>
- <priority>0.6</priority>
- </url>
- <?php } // end foreach ?>
- </urlset>
复制上面代码为xmlmap.php文件并传至网站根目录:
http://localhost/xmlmap.php
新闻热点
疑难解答
图片精选