wordpress生成sitemap.txt类型文件的方法

市面上都是安装插件,然后生成sitemap.xml或者sitemap.html,就是没有生成sitemap.txt类型的。于是百度整理了一下方法,发现以下代码亲测可行。

<?php
require('./wp-blog-header.php');
header("Content-type: text/txt");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000; // 获取文章数量
?>
<?php echo 'http://'.$_SERVER['HTTP_HOST']; ?><?php echo "\n"; ?>
<?php
/* 文章页面 */
header("Content-type: text/txt");
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) {
the_permalink();
echo "\n";
}
?>
<?php
/* 单页面 */
$mypages = get_pages();
if(count($mypages) > 0) {
foreach($mypages as $page) {
echo get_page_link($page->ID);
echo "\n";
}
}
?>
<?php
/* 博客分类 */
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$count = count($terms);
if($count > 0){
foreach ($terms as $term) {
echo get_term_link($term, $term->slug);
echo "\n";
}
}
?>
<?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;
}
echo $link;
echo "\n";
}
?>

具体使用方法,在根目录创建一个php文件。命名随意。

然后把代码复制到该php文件中。如 sitemap.php

wordpress生成sitemap.txt类型文件的方法
txt

最后访问该地址就会生成。要生成的数量和栏目可在代码里自行修改。

公众号:懒汉运营,关注获取免费资源。如若转载本文章,请注明出处:https://98seo.com/wangzhan/440.html

(2)
上一篇 2021年11月19日 下午2:20
下一篇 2021年11月25日 上午10:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

微信
微信关注公众号:「懒汉运营」 ,懒汉运营最新官网:www.lanxhan.com