国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

wordpress無插件生成文章TXT網站地圖的方法

2024-09-07 00:52:20
字體:
來源:轉載
供稿:網友

我們知道網站地址有利于搜索引擎的收錄,wordpress有自帶的xml格式的網站地圖,如果我們也希望生成txt格式的地圖呢?本文我們用純代碼方法來實現.

該方法不需要安裝任何插件,純代碼生成.

  1. <?php 
  2. require('./wp-blog-header.php'); 
  3. header('Content-type: application/txt'); 
  4. header('HTTP/1.1 200 OK'); 
  5. $posts_to_show = 50000; // 限制最大文章數量 
  6. ?> 
  7. <?php 
  8. header("Content-type: text/txt"); 
  9. $myposts = get_posts( "numberposts=" . $posts_to_show ); 
  10. <a href="/tags.php/foreach/" target="_blank">foreach</a>( $myposts as $post ) {  
  11. //Vevb.com 
  12. ?> 
  13. <?php the_permalink(); ?><?php echo "/n"; ?> 
  14. <?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代碼:

  1. <?php 
  2. require('./wp-blog-header.php'); 
  3. header("Content-type: text/xml"); 
  4. header('HTTP/1.1 200 OK'); 
  5. $posts_to_show = 1000;  
  6. echo '<?xml version="1.0" encoding="UTF-8"?>'
  7. 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/">' 
  8. ?> 
  9. <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>--> 
  10.   <url> 
  11.       <loc><?php echo get_home_url(); ?></loc> 
  12.       <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> 
  13.       <changefreq>daily</changefreq> 
  14.       <priority>1.0</priority> 
  15.   </url> 
  16. <?php 
  17. /* 文章頁面 */  
  18. header("Content-type: text/xml"); 
  19. $myposts = get_posts( "numberposts=" . $posts_to_show ); 
  20. foreach$myposts as $post ) { ?> 
  21.   <url> 
  22.       <loc><?php the_permalink(); ?></loc> 
  23.       <lastmod><?php the_time('c') ?></lastmod> 
  24.       <changefreq>monthly</changefreq> 
  25.       <priority>0.6</priority> 
  26.   </url> 
  27. <?php } /* 文章循環結束 */ ?>   
  28. <?php 
  29. /* 單頁面 */  
  30. $mypages = get_pages(); 
  31. if(count($mypages) > 0) { 
  32.     foreach($mypages as $page) { ?> 
  33.     <url> 
  34.       <loc><?php echo get_page_link($page->ID); ?></loc> 
  35.       <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod> 
  36.       <changefreq>weekly</changefreq> 
  37.       <priority>0.6</priority> 
  38.   </url> 
  39. <?php }} /* 單頁面循環結束 */ ?>  
  40. <?php 
  41. /* 博客分類 */  
  42. $terms = get_terms('category''orderby=name&hide_empty=0' ); 
  43. $count = count($terms); 
  44. if($count > 0){ 
  45. foreach ($terms as $term) { ?> 
  46.     <url> 
  47.       <loc><?php echo get_term_link($term$term->slug); ?></loc> 
  48.       <changefreq>weekly</changefreq> 
  49.       <priority>0.8</priority> 
  50.   </url> 
  51. <?php }} /* 分類循環結束 */?>  
  52. <?php 
  53.  /* 標簽(可選) */ 
  54. $tags = get_terms("post_tag"); 
  55. foreach ( $tags as $key => $tag ) { 
  56.     $link = get_term_link( intval($tag->term_id), "post_tag" ); 
  57.          if ( is_wp_error( $link ) ) 
  58.           return false; 
  59.           $tags$key ]->link = $link
  60. ?> 
  61.  <url> 
  62.       <loc><?php echo $link ?></loc> 
  63.       <changefreq>monthly</changefreq> 
  64.       <priority>0.4</priority> 
  65.   </url> 
  66. <?php  } /* 標簽循環結束 */ ?>  
  67. </urlset> 

wordpress非插件實現xml格式網站地圖.

  1. <?php 
  2. require('./wp-blog-header.php'); 
  3. header("Content-type: text/xml"); 
  4. header('HTTP/1.1 200 OK'); 
  5. $posts_to_show = 1000; // 獲取文章數量 
  6. echo '<?xml version="1.0" encoding="UTF-8"?>'
  7. 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>" 
  8. 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>; 
  9. ?> 
  10. <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>--> 
  11. <url> 
  12. <loc>http://localhost/</loc> 
  13. <lastmod><?php echo get_lastpostdate('blog'); ?></lastmod> 
  14. <changefreq>daily</changefreq> 
  15. <priority>1.0</priority> 
  16. </url> 
  17. <?php 
  18. header("Content-type: text/xml"); 
  19. $myposts = get_posts( "numberposts=" . $posts_to_show ); 
  20. foreach$myposts as $post ) { ?> 
  21. <url> 
  22. <loc><?php the_permalink(); ?></loc> 
  23. <lastmod><?php the_time('c') ?></lastmod> 
  24. <changefreq>monthly</changefreq> 
  25. <priority>0.6</priority> 
  26. </url> 
  27. <?php } // end foreach ?> 
  28. </urlset> 

復制上面代碼為xmlmap.php文件并傳至網站根目錄:

http://localhost/xmlmap.php

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜陵市| 姚安县| 平遥县| 延川县| 新余市| 左云县| 松江区| 嘉兴市| 遵化市| 元阳县| 砀山县| 辽阳市| 本溪| 兴安盟| 满洲里市| 苏州市| 枣庄市| 渝北区| 且末县| 新邵县| 张家界市| 剑阁县| 柘城县| 博爱县| 广丰县| 甘洛县| 铜川市| 明溪县| 新闻| 潍坊市| 韶山市| 麻栗坡县| 波密县| 峨边| 红桥区| 义马市| 乌苏市| 淄博市| 天津市| 清远市| 台中市|