加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (https://www.0391zz.cn/)- 数据工具、数据仓库、行业智能、CDN、运营!
当前位置: 首页 > 教程 > 正文

wordpress无插件生成文章TXT网站地图的方法介绍

发布时间:2022-06-22 17:12:05 所属栏目:教程 来源:互联网
导读:我们知道网站地址有利于搜索引擎的收录,wordpress有自带的xml格式的网站地图,如果我们也希望生成txt格式的地图呢?本文我们用纯代码方法来实现. 该方法不需要安装任何插件,纯代码生成. ?php require(./wp-blog-header.php); header(Content-type: application
  我们知道网站地址有利于搜索引擎的收录,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 ) {  
  //phpfensi.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文件,只需要右键另存为即可.

  /* 文章页面 */  
  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');

  复制上面代码为xmlmap.php文件并传至网站根目录:
 
  http://localhost/xmlmap.php。
 
 

(编辑:济源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读