一段代码实现Sitemap站点地图


分享一段简单的代码,帮你快速生成网站的Sitemap站点地图。Sitemap是一个列出网站上所有页面URL的文件,它帮助搜索引擎更好地索引你的网站内容。

文章中的代码示例将指导你如何自动化生成Sitemap,这对于提高网站的SEO(搜索引擎优化)非常有用。无论你的网站大小,这个技巧都能帮助你确保搜索引擎能够找到并正确索引你的网页。

具体操作

1.新建一个php模板页面

在你的主题目录下新建一个 sitemap.php 模板页面(名称可自定义),然后复制以下代码到其中:

<?php
/**
*Sitemap网站地图
*
* @package custom
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
  <meta http-equiv="Content-Type" content="text/html; charset=<?php $this->options->charset(); ?>" />
  <title>站点地图 - <?php $this->options->title() ?></title>
  <meta name="keywords" content="站点地图,<?php $this->options->title() ?>" />
  <meta name="copyright" content="<?php $this->options->title() ?>" />
  <link rel="canonical" href="<?php $this->permalink() ?>" />
  <style type="text/css">
    body {font-family: Microsoft Yahei,Verdana;font-size:13px;margin:0 auto;color: #000000;background: #ffffff;width: 990px;margin: 0 auto}
    a:link,a:visited {color:#000;text-decoration:none;}
    a:hover {color:#08d;text-decoration:none;}
    h1,h2,h3,h4,h5,h6 {font-weight:normal;}
    img {border:0;}
    li {margin-top: 8px;}
    .page {padding: 4px; border-top: 1px #EEEEEE solid}
    .author {background-color:#EEEEFF; padding: 6px; border-top: 1px #ddddee solid}
    #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 $this->options->title() ?>'s SiteMap </h2>
  <center></center>
  <div id="nav"><a href="<?php $this->options ->siteUrl(); ?>"><strong><?php $this->options->title() ?></strong></a> » <a href="<?php $this->permalink() ?>">站点地图</a></div>
  <div id="content">
    <h3>最新文章</h3>
    <ul>
      <?php
      $stat = Typecho_Widget::widget('Widget_Stat');
      $this->widget('Widget_Contents_Post_Recent', 'pageSize='.$stat->publishedPostsNum)->to($archives);
      $year=0; $mon=0; $i=0; $j=0;
      while($archives->next()){
          $year_tmp = date('Y',$archives->created);
          $mon_tmp = date('m',$archives->created);
          $y=$year; $m=$mon;
          if ($year > $year_tmp || $mon > $mon_tmp) {
              $output .= '</ul>';
          }
          $output .= '<li><a href="'.$archives->permalink .'">'. $archives->title .'</a></li>';
      }
      $output .= '</ul>';
      echo $output;
      ?>
    </ul>
  </div>
  <div id="content">
    <li class="categories">分类目录
      <ul><?php $this->widget('Widget_Metas_Category_List')
               ->parse('<li><a href="{permalink}">{name}</a> ({count})</li>'); ?>
      </ul>
    </li>
  </div>
  <div id="content">
    <li class="categories">独立页面</li>
    <ul class="clearfix" id="nav_menu">
      <li><a href="<?php $this->options->siteUrl(); ?>">首页</a></li>
      <?php $this->widget('Widget_Contents_Page_List')
                 ->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
    </ul>
  </div>
  <div id="footer">查看博客首页: <strong><a href="<?php $this->options ->siteUrl(); ?>"><?php $this->options->title() ?></a></strong></div><br />
  <center>
    <div style="text-algin: center; font-size: 11px"><br /> © <?php echo date('Y'); ?> <strong><a href="<?php $this->options ->siteUrl(); ?>" target="_blank"><?php $this->options->title() ?></a></strong> 版权所有<br /><br /><br />
    </div>
  </center>
</body>
</html>

2.创建新页面

登录你的网站后台,创建一个新的页面,模板选择上面新创建的 Sitemap网站地图 (其他地方可以不用管,直接发布即可)

实际情况请根据你自己的主题进行微调。

3.打开刚发布的页面,即可看到属于你自己的Sitemap网站地图。

完!


文章作者: jacktang
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 jacktang !
评论
评论
 上一篇
网站链接跳转提示 网站链接跳转提示
这篇文章将教会你如何为你的网站添加外部链接跳转提示功能。当访问者点击一个指向其他网站的链接时,这个功能会先显示一个警告框,提醒他们即将离开当前网站。这样做不仅可以提高用户体验,还能帮助访问者意识到他们即将访问外部内容,从而增加网站的安全性和透明度。文章会详细讲解设置过程,让你轻松为自己的网站加入这一实用功能。
2024-05-04
下一篇 
  目录