服务器与VPS
促销优惠信息

wordpress纯代码给网站添加HTML网站地图

我们都知道网站地图对于站点的搜索引擎优化很有好处,一版来说默认的Sitemap都是XML格式,这样便于蜘蛛对于网站内容的爬取,不过除了SEO方面的需求,一个条理分明的网站地图对于用户浏览和站长自己梳理网站历程也很有好处,所以除了XML版的网站地图,很多站长也会给网站添加HTML版网站地图。

一般来说,大家都是通过sitemap插件或者SEO插件来生成网站地图,但是其实我们完全可以自己通过纯代码来给wordpress网站添加html版的站点地图。

 

一、给网站添加一个sitemap页面模板:

1、创建sitemap.php文件

2、将以下代码放入这个文件中

<?php
/*
Template Name: HTML-SITEMAP
*/
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTML Sitemaps</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<body <?php body_class('m-3');?>>
  <div class="container">
    <h1 class="fs-3">Sitemap</h1>
    <div class="item">
      <h2 class="fs-4">Posts</h2>
      <ul class="lh-lg">
        <?php
        $the_query = new WP_Query(
          array(
            'post_type' => 'post',
            'posts_per_page' => -1,
            'orderby' => 'date',
            'order' => 'DESC'
          )
        );
        if ($the_query->have_posts()) :
          while ($the_query->have_posts()) : $the_query->the_post();
            ?>
            <li><a href="<?php%20the_permalink();%20?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
          <?php endwhile; else : ?>
            <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
          <?php endif; ?>
      </ul>
    </div>
    <!-- .item -->
    <div class="item">
      <h2 class="fs-h5">Pages</h2>
      <ul class="lh-lg">
        <?php
        $the_query = new WP_Query(
          array(
            'post_type' => 'page',
            'posts_per_page' => -1,
            'orderby' => 'date',
            'order' => 'DESC'
          )
        );
        if ($the_query->have_posts()) :
          while ($the_query->have_posts()) : $the_query->the_post();
            ?>
            <li><a href="<?php%20the_permalink();%20?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
          <?php endwhile; else : ?>
            <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
          <?php endif; ?>
      </ul>
  </div>
  <!-- .item -->
  <div class="item">
    <h2 class="fs-h5">Categories</h2>
    <ul class="lh-lg">
      <?php wp_list_categories(
        array(
          'title_li' => '',
          'show_count' => true,
          'hide_empty' => false,
          'orderby' => 'name',
          'order' => 'ASC'
        )
      ); ?>
    </ul>
  </div>
  <!-- .item -->
  <?php echo '<!-- ' . get_num_queries() . ' queries in ' . timer_stop( 0, 5 ) . ' seconds. -->'; ?>
</body>
</html>

注:<html lang="zh-CN">说明站点地图是中文的,如果是做外贸独立站的朋友可以把”zh-CN“替换成”en-US“。

3、将sitemap.php文件上传到当前主题的页面文件夹,通常是(pages)

4、回到网站后台——页面——新建页面——填写页面标题——在右侧选择”sitemap“模板,发布。

然后,你的wordpress就会自动生成html网站地图。

以下是本站的示例页面:https://www.chukuangren.com/sitemap

 

楚狂人 » wordpress纯代码给网站添加HTML网站地图

相关推荐

  • 暂无文章

评论 抢沙发

  • (必填)
  • (必填)

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏