#精品
如何用纯代码制作html和xml版本的网站地图

2021-08-11 0 213 百度已收录

站点地图站点地图分为XML版本和HTML版本,XML版本的站点地图主要用于搜索引擎。HTML版本是为了提升用户体验,让用户看到网站的内容和分类。本文主要教大家如何不用插件制作网站地图。

一、制作html版本的站点地图

  1. <?php
    /*
    Template Name: Sitemap
    */
    ?>
    <!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 bloginfo( 'charset' ); ?>" />
    <title>站点地图 - <?php bloginfo('name'); ?></title>
    <meta name="keywords" content="站点地图,<?php bloginfo('name'); ?>" />
    <meta name="copyright" content="<?php bloginfo('name'); ?>" />
    <link rel="canonical" href="<?php echo get_permalink(); ?>" rel="external nofollow"  rel="external nofollow"  />
    <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 bloginfo('name'); ?>'s SiteMap </h2>
    <center></center>
    <div id="nav"><a href="<?php bloginfo('url'); ?>/" rel="external nofollow"  rel="external nofollow" ><strong><?php bloginfo('name'); ?></strong></a> &raquo; <a href="<?php echo get_permalink(); ?>" rel="external nofollow"  rel="external nofollow" >站点地图</a></div>
    <div id="content">
    <h3>最新文章</h3>
    <ul>
    <?php
    $previous_year = $year = 0;
    $previous_month = $month = 0;
    $ul_open = false;
    $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
    foreach($myposts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>" rel="external nofollow"  title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </div>
    <div id="content">
    <li class="categories">分类目录<ul>
    <?php wp_list_categories('title_li='); ?>
    </ul></li>
    </div>
    <div id="content">
    <li class="categories">单页面</li>
    <?php wp_page_menu( $args ); ?>
    </div>
    <div id="footer">查看博客首页: <strong><a href="<?php bloginfo('url'); ?>/" rel="external nofollow"  rel="external nofollow" ><?php bloginfo('name'); ?></a></strong></div><br />
    <center>
    <div style="text-algin: center; font-size: 11px"><strong><a href="https://www.zhanceo.com/sitemap_baidu.xml" target="_blank">Baidu-SiteMap</a></strong> Latest Update: <?php $last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");$last = date('Y-m-d G:i:s', strtotime($last[0]->MAX_m));echo $last; ?><br /><br /></div>
    </center>
    </body>
    </html>
    

     

将上述文件保存到 sitemap.php,上传到主题目录,打开 wordpress 后台,新建页面,模板选择”sitemap”,发布。记住这个页面的链接,将这个链接放到 footer 或者其它地方。

如何用纯代码制作html和xml版本的网站地图

  1. 二、制作xml版本的站点地图
  2. <?php
    require('./wp-blog-header.php');
    header("Content-type: text/xml");
    header('HTTP/1.1 200 OK');
    $posts_to_show = 1000;
    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'); ?> -->
    <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
    /* 文章页面 */
    header("Content-type: text/xml");
    $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
    /* 博客分类 www.stcash.com*/
    $terms = get_terms('category', 'orderby=name&hide_empty=0' );
    $countcount = 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>
    

     

将以上代码保存到 sitemap.php,上传到网站根目录,并重写.htaccess,在第一行加上一句代码:

  1. RewriteRule ^(sitemap)\.xml$ $1.php

这句代码的意思是将 php 文件重写为 xml 文件,ok,xml 制作完毕。

至此,xml 版本的 html 版本的站点地图已经制作成功。再次提醒大家,我们在制作过程中要注意两个 sitemap.php 上传的目录是不一样的,不要弄混淆。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明   ⚠️ 1. 本站所有资源来源于网络收集,若资源侵犯了您的合法权益, 请来信通知我们(Email: 1427707223@qq.com),我们会及时删除,给您带来的不便,我们深表歉意! 2. 分享目的仅供大家学习和交流,若使用商业用途,请购买正版授权! 否则产生的一切后果将由下载用户自行承担! 3. 会员不得使用于非法商业用途,不得违反国家法律。否则后果自负! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 6. 因源码具有可复制性,一经购买 ,不得以任何形式退款。 7.更多详情请点击查看

优站网 WordPress 如何用纯代码制作html和xml版本的网站地图 https://www.zhanceo.com/14894.html

诚乃立身之本、信为道德之基

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

联系官方客服

为您解决烦忧 - 24小时在线 专业服务

如何用纯代码制作html和xml版本的网站地图-海报