我們知道網站地址有利于搜索引擎的收錄,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
|
新聞熱點
疑難解答
圖片精選