温馨提示:本文最后更新于
2024-11-26 10:16:05
,某些文章具有时效性,若有错误或已失效,请在下方留言目前新版的wordpress已经自带了XML站点地图,可以在地址栏直接访问,但是自带的站点地图属于索引型,可以简单的理解为xml文档里面还有xml文档,而不是真正的网站网址,这种索引型通过sitemap提交的话百度不会收录,会提示“索引型不予处理”,所以本章教大家如何不使用插件的情况下生成支持百度收录的xml站点地图。
操作方法:
在网站根目录下新建一个php文件,名称自定义,我这里以sitemap.php为例,内容见下方(无需任何更改)
<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 9999;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?> 极客创益(https://geeke.jikeboss.cn/)-->
<url>
<loc><?php echo get_home_url(); ?></loc>
<lastmod><?php $ltime = get_lastpostmodified('GMT');$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php
/* 输出普通文章 POST */
$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>
然后再在宝塔中添加一个计划任务,代码中的路径替换成你自己的。任务类型选择shell脚本,执行周期自定义。完成后可点执行测试,如果看到根目录下生成了sitemap.xml表示成功。
© 版权声明
THE END
暂无评论内容