1. 程式人生 > >免外掛實現WordPress的Sitemap功能

免外掛實現WordPress的Sitemap功能

生成 Sitemap 的外掛有很多,比較知名的有柳城的 Baidu Sitemap Generator 和 Google XML Sitemaps,感覺就為了生成個 Sitmap 多用外掛有點點浪費,那就用程式碼實現吧~缺點在於此為動態頁面,提交網站地圖時不知道會不會有困難。

新建一個 sitemap.php 至主題目錄下,複製如下內容:

PHP
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 <?php/**@package WordPressTemplate 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=<?phpbloginfo('charset');?>"/><title>站點地圖-<?phpbloginfo('name');?></title><meta name="keywords"content="站點地圖,<?phpbloginfo
('name');?>"
/><meta name="copyright"content="<?phpbloginfo('name');?>"/><link rel="canonical"href="<?phpechoget_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#EEEEEEsolid}.author {background-color:#EEEEFF;padding:6px;border-top:1px#ddddeesolid}#nav, #content, #footer {padding:8px;border:1pxsolid#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"><?phpbloginfo('name');?>'sSiteMap</h2><center></center><div id="nav"><ahref="<?phpbloginfo('url');?>/"><strong><?phpbloginfo('name');?></strong></a>&raquo;<ahref="<?phpechoget_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($mypostsas$post):?><li><ahref="<?phpthe_permalink();?>"title="<?phpthe_title();?>"target="_blank"><?phpthe_title();?></a></li><?phpendforeach;?></ul></div><div id="content"><li class="categories">分類目錄<ul><?phpwp_list_categories('title_li=');?></ul></li></div><div id="content"><li class="categories">單頁面</li><?phpwp_page_menu($args);?></div><div id="footer">檢視部落格首頁:<strong><ahref="<?phpbloginfo('url');?>/"><?phpbloginfo('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><ahref="http://www.kilvn.com/view/wordpress-no-plugins-sitemap"target="_blank">免外掛SiteMap</a></strong>&nbsp;&copy;<?phpechodate('Y');?><ahref="<?phpbloginfo('url');?>/"style="cursor:help"><?phpbloginfo('name');?></a>版權所有.<br/><br/></div></center></body></html>

在 WP 後臺新建一個頁面,型別為:站點地圖,不用填寫任何內容,點擊發布即可。