生成 Sitemap 的插件有很多,比较知名的有柳城的 Baidu Sitemap Generator 和 Google XML Sitemaps,感觉就为了生成个 Sitmap 多用插件有点点浪费,那就用代码实现吧~缺点在于此为动态页面,提交网站地图时不知道会不会有困难。
新建一个 sitemap.php 至主题目录下,复制如下内容:
<?php /** @package WordPress Template Name: 站点地图 */ ?> <!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: Verdana;FONT-SIZE: 12px;MARGIN: 0;color: #000000;background: #ffffff;} 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> » <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">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> <center> <div style="text-algin: center; font-size: 11px">Powered by <strong><a href="/goto/aHR0cDovL3d3dy5raWx2bi5jb20vdmlldy88YSBocmVmPQ==" target="_blank"https://zhang.ge/tag/wordpress/" class="atags color-2" title="View all posts in wordpress" target="_blank">wordpress-no-plugins-sitemap" target="_blank">免插件 SiteMap</a></strong> © <?php echo date('Y'); ?> <a href="<?php bloginfo('url');?>/" style="cursor:help"><?php bloginfo('name');?></a> 版权所有.<br /><br /></div> </center> </body> </html>
在 WP 后台新建一个页面,类型为:站点地图,不用填写任何内容,点击发布即可。
博主已经用上了,又删掉了一个插件,真开心!https://zhang.ge/blogmap
大侠那个SEO软件的确只能生成XML,之前我都是利用baidu那个插件生成HTML在把里面的内容COPY到sitemap.php进行手动更新。
文章不是提供了源码么?你试下不就知道了?
嗯呢,已经用了。我的意识是之前是那么做的哈、
我也用上了,谢谢~
呵呵 很好的工具 收录了
如何制作纯链接的TXT格式地址呢?链接是伪静态的。怎么弄,求助博主
临时给你写了个代码,记得多多支持、多多宣传Jager博客,点点广告什么的,还有留言还是填写自己的网站地址,也让别人了解一下你,藏着捏着干嘛呢,是吧?
以下代码保存为 php文件,比如 sitemap_txt.php,放到网站根目录即可访问:
访问路径即 http://你的网站/sitemap_txt.php ,伪静态什么的就自己研究吧!
谢谢博主。虽然不太懂,让我在研究多一会~
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in /data/home/qxu1192050280/htdocs/wp-includes/meta.php on line 575 错误 不知道为什
这个方法是不是仅仅针对贵站的模板呢,我表示我新建页面中并没有类型为站点模板的可选项
你复制好生成一个sitemap.php 放到主题目录才可以啊 亲
表示后缀xml的怎么搞
https://zhang.ge/4554.html
张哥,访问https://zhang.ge/blogmap 则变成https://zhang.ge/blogmap.html
你是用的伪静态还是301重定向?
可以贴代码吗?
这个代码加到哪个文件里?
functions.php
博主您好,把上面的代码放入Functions.php后,访问http://i.cuixt.com/sitemap并没有变成http://i.cuixt.com/sitemap.html,如果手动访问http://i.cuixt.com/sitemap.html则显示404,怎么办呢? :?:
签到成功!签到时间:下午2:00:55,每日签到,生活更精彩哦~
试过了,没效果啊签到成功!签到时间:上午9:49:15,每日签到,生活更精彩哦~
正找这个呢,太有用了,谢谢张哥
太好用了,非常感谢!
刚才尝试了下在开头 加入这句,然后在利用这篇文章的方法https://zhang.ge/4554.html 这样就可以实现了静态文件了!感谢博主的文章!
最近怎么没法评论了
已收藏。。。赞一个
博主,可以帮忙看下这个问题吗?
我想实现的这样的功能,就是打开php生成sitemap.html,有点类似这篇文章的https://zhang.ge/4554.html 然后每天定时去生成
然后做了就把你这篇文章的代码就做了一处修改。如下图红框一那里
刚开始文章数不是很多,没有发现问题,近来发现生成空白页面,于是尝试修改图片中红框2的numberposts的值为几千又可以生成了,所以只好跑过来咨询博主看下这个问题,还望博主抽出宝贵的时间看下,HTTP报错500,谢谢!
刚才记得插入进去了,怎么没有了!麻烦博主了
文章数量太多了吧?PHP超时了