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

作者:袖梨 2022-06-25

该方法不需要安装任何插件,纯代码生成。

<?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 );
foreach( $myposts as $post ) { ?>
<?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.a*d*m*in.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 'ps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.*b*a*idu.com/schemas/sitemap-mobile/1/">'
?>

  
      <?php echo get_home_url(); ?>
      <?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-dTH:i:s+00:00', strtotime($ltime)); echo $ltime; ?>
      daily
      1.0
  
<?php /* 文章页面 */ 
header("Content-type: text/xml");
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
  
      <?php the_permalink(); ?>
      <?php the_time('c') ?>
      monthly
      0.6
  
<?php } /* 文章循环结束 */ ?>  
<?php /* 单页面 */ 
$mypages = get_pages();
if(count($mypages) > 0) {
    foreach($mypages as $page) { ?>
    
      <?php echo get_page_link($page->ID); ?>
      <?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00
      weekly
      0.6
  
<?php }} /* 单页面循环结束 */ ?> 
<?php /* 博客分类 */ 
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$count = count($terms);
if($count > 0){
foreach ($terms as $term) { ?>
    
      <?php echo get_term_link($term, $term->slug); ?>
      weekly
      0.8
  
<?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;
?>
 
      <?php echo $link ?>
      monthly
      0.4
  
<?php } /* 标签循环结束 */ ?> 



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 'http://www.*w**3.org/2001/XMLSchema-instance" xmlns=""
xsi:schemaLocation=" ;
?>


http://l**o*calhost/
<?php echo get_lastpostdate('blog'); ?>
daily
1.0

<?php header("Content-type: text/xml");
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>

<?php the_permalink(); ?>
<?php the_time('c') ?>
monthly
0.6

<?php } // end foreach ?>



复制上面代码为xmlmap.php文件并传至网站根目录
http://l**o*calhost/xmlmap.php

相关文章

精彩推荐