当前位置: 首页 » 教程 » WordPress非插件创建sitemap文件sitemap.xml和sitemap.html

WordPress非插件创建sitemap文件sitemap.xml和sitemap.html

零分教程2,3252023-02-04 19:55

sitemap.xml和sitemap.html,Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页。最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度为何等),以便搜索引擎可以更加智能地抓取网站。

WordPress非插件创建方法:

1、在主题目录下 创建一个page页面文件,命名page-sitemap.php,代码:

<?php
/**
Template Name: 站点地图
*作者:零分
*QQ :87087126
*博客://blog.32xp.com
*下载://www.32xp.com
*生成站点地图HTML和XML
**/
$id=isset($wp_query->query_vars['id']) && $wp_query->query_vars['id']==1 ? true:false;
if($id){
header("Content-Type:text/xml");
echo '<?xml version="1.0" encoding="utf-8"?>';
$xml.='<urlset xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns="//www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="//www.sitemaps.org/schemas/sitemap/0.9 //www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
$xml.='<url>';
$xml.='<loc>'.get_bloginfo('url').'</loc>';
$xml.="<lastmod>".date('Y-m-d H:i',time())."</lastmod>";
$xml.='<priority>0.8</priority>';
$xml.='</url>';
$xml.='<url>';
$xml.='<loc>'.get_bloginfo('url').'/sitemap.html</loc>';
$xml.="<lastmod>".date('Y-m-d H:i',time())."</lastmod>";
$xml.='<priority>0.8</priority>';
$xml.='</url>';
query_posts('showposts=30000');
while (have_posts()) : the_post();
$xml.='<url>';
$xml.='<loc>'.get_the_permalink().'</loc>';
$xml.="<lastmod>".date('Y-m-d H:i',get_the_time('U'))."</lastmod>";
$xml.='<priority>0.8</priority>';
$xml.='</url>';
endwhile;wp_reset_query();
foreach(get_categories() as $cat){
$xml.='<url>';
$xml.='<loc>'.get_category_link($cat->term_id).'</loc>';
$xml.="<lastmod>".date('Y-m-d H:i',time())."</lastmod>";
$xml.='<priority>0.8</priority>';
$xml.='</url>';
}
foreach(get_tags() as $tag){
$xml.='<url>';
$xml.='<loc>'.get_tag_link($tag->term_id).'</loc>';
$xml.="<lastmod>".date('Y-m-d H:i',time())."</lastmod>";
$xml.='<priority>0.8</priority>';
$xml.='</url>';
}
$xml.='</urlset>';
echo $xml;
}else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="//www.w3.org/1999/xhtml">
<head profile="//gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php bloginfo( "name" ); ?> - 站点地图</title>
<meta name="keywords" content="站点地图,<?php bloginfo( "name" ); ?>" />
<meta name="generator" content="Baidu SiteMap Generator" />
<meta name="author" content="LingFen" />
<meta name="copyright" content="<?php bloginfo( "name" ); ?>" />
<style type="text/css">
body {font-family: Verdana;FONT-SIZE: 12px;MARGIN: 0;color: #000000;background: #ffffff;}
img {border:0;}
a{text-decoration:none;}
a:hover{text-decoration:underline;}
li {margin-top: 8px;}
.page {padding: 4px; border-top: 1px #EEEEEE solid}
.author {background-color:#EEEEFF; padding: 6px; border-top: 1px #ddddee solid}
.tag a{display:inline-block;margin-right:10px;padding:3px 0;}
#nav, #content, #footer {padding: 8px; border: 1px solid #EEEEEE; clear: both; width: 95%; margin: auto; margin-top: 10px;}
</style>
</head>
<body vlink="#333333" link="#333333">
<h2 style="text-align: center; margin-top: 20px"><?php bloginfo( "name" ); ?>&#39;s SiteMap </h2>
<center></center>
<div id="nav"><a href="<?php bloginfo( "url" ); ?>"><strong><?php bloginfo( "name" ); ?></strong></a> » <a href="<?php bloginfo( "url" ); ?>/sitemap.html">站点地图</a></div>
<div id="content">
<h3>最新文章</h3>
<ul>
<?php query_posts('showposts=500');while (have_posts()) : the_post();?><li><a href="<?php the_permalink(); ?>" title="<?php the_title();?>" target="_blank"><?php the_title();?></a></li><?php endwhile;wp_reset_query();?>
</ul>
</div>
<div id="content">
<h3>分类目录</h3>
<ul>
<?php foreach(get_categories() as $cat){ if($cat->parent==0){?>
<li><a target="_blank" href="<?php echo get_category_link($cat->term_id);?>" title="<?php echo $cat->name;?>"><?php echo $cat->name;?></a>
<?php foreach(get_categories() as $ccat){ if($ccat->parent==$cat->term_id){?>
<ul><li><a target="_blank" href="<?php echo get_category_link($ccat->term_id);?>" title="<?php echo $ccat->name;?>"><?php echo $ccat->name;?></a></li></ul>
<?php } } ?>
</li>
<?php } } ?>
</ul>
</div>
<div id="content" class="tag">
<h3>TAG标签</h3>
<?php wp_tag_cloud('number=500&unit=px');?>
</div>
<div id="footer">查看首页: <strong><a href="<?php bloginfo( "url" ); ?>"><?php bloginfo( "name" ); ?></a></strong></div><br />
<center>
<div style="text-algin: center; font-size: 11px">
<strong><a href="<?php bloginfo( "url" ); ?>/sitemap.html">SiteMap.html</a>
<a href="<?php bloginfo( "url" ); ?>/sitemap.xml">SiteMap.xml</a></strong>&nbsp;&nbsp; Latest Update: <?php echo date("Y-m-d H:i:s",time());?><br /><br /></div>
</center>
<center>
<div style="text-algin: center; font-size: 11px">Powered by <strong><a href="<?php bloginfo( "url" ); ?>" target="_blank">
Baidu SiteMap Generator</a></strong>&nbsp;© 2008-<?php echo date("Y",time());?> <a target="_blank" href="<?php bloginfo( "url" ); ?>" title="零分">
零分</a><br /><br /></div>
</center>
</body>
</html>
<?php }?>

2、在WordPress后台创建一个页面文件,选择模板站点地图

3、设置伪静态,在functions.php添加设置伪静态函数

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );

if ( ! isset( $rules['sitemap.xml'] ) || ! isset( $rules['sitemap.html'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['sitemap.xml$'] = 'index.php?pagename=sitemap&id=1';
$newrules['sitemap.html$'] = 'index.php?pagename=sitemap&id=$matches[1]';
return $newrules + $rules;
}
function my_insert_query_vars( $vars )
{
array_push($vars, 'id');
return $vars;
}

代码已打包百度网盘下载:WordPress-sitemap(提取码:b8rh,大小:34.1kb)

END
零分站龄17年资深站长
一个喜欢折腾,却又折腾不出像样东西的,不会PHP的PHP程序员!
2513
文章
13
分类
3813
标签
3
友链
onlinelovesky317355746vipsever@vip.qq.com