WordPress网站如何实现纯代码制作站点地图文件(html和xml版本)

WordPress国产主题推荐

WordPress网站的地图文件一般分为两种格式:一是XML格式,也是最多人使用的格式;二是HTML格式,一般是放在网站首页或页脚给用户看的地图文件。

如果你想要让自己的WordPress网站同时生成XML和HTML两种格式的地图文件,可以考虑XML Sitemap Generator for Google插件。

当然,如果你不喜欢使用WordPress插件实现,想要自己通过代码实现,也是没问题的,只需要根据以下步骤添加相应的地图文件就可以了。

一、制作WordPress网站的Html地图文件

第一步:将以下代码复制到一个PHP文件中,文件名随意,如htmlsitemap.php,并将这个文件上传到自己站点所使用的主题文件夹内。如boke112百科使用的是YIA主题,那么就上传到/wp-content/themes/yia/文件夹内。

<?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(); ?>" />
<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'); ?>/"><strong><?php bloginfo('name'); ?></strong></a> &raquo; <a href="<?php echo get_permalink(); ?>">站点地图</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(); ?>" 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'); ?>/"><?php bloginfo('name'); ?></a></strong></div><br />
<center>
<div style="text-algin: center; font-size: 11px"><strong><a href="http://www.timle.cn/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>

第二步:进入WordPress后台新建一个页面,页面标题随意,如boke112百科html地图,然后在“页面属性 – 模板”中选择“Sitemap”,并点击【发布】按钮,最后点击固定链接的【编辑】按钮,将固定链接从中文改成拼音或英文,并更新文件。

WordPress网站如何实现纯代码制作站点地图文件(html和xml版本)-第1张-boke112百科(boke112.com)

第三步:访问刚才创建的Html地图页面,看看是否正常?若正常就将链接添加到自己站点的页脚或导航菜单,或其他合适的地方,让来访的用户可以点击访问。

二、制作WordPress网站的XML地图文件

第一步:将以下代码复制到一个PHP文件中,文件名随意,如xmlsitemap.php,并将这个文件上传到自己站点根目录中。

<?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>

第二步:在自己网站首页的URL地址后面添加上第一步创建的文件名看看是否正常?若正常再考虑将PHP文件重写成xml。

①Nginx 做法

在原来的伪静态位置新增规则:

rewrite ^/xmlsitemap.xml$ /xmlsitemap.php last;

②Apache 做法

在 .htaccess 中新增规则:

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

以上代码中的xmlsitemap就是第一步所创建的文件名,记得修改哦。之后网站地图文件就变成了xmlsitemap.xml,你也可以在浏览器中访问看看效果。

特别提醒:文中生成的HTML和XML代码均来自网络。

5/5 - (1 vote)
文章创作不易,期待您的评分

本文地址:https://boke112.com/article/p1537.html

版权声明:本文内容来源于互联网资源,由 boke112百科 整理汇总!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu