wordpress添加站点地图页面_存档页面

12-31 | 夜光 | IT记录

wordpress添加站点地图页面:

一、在主题functions.php文件中添加如下代码:

/*站点地图_改完这段代码以后,要增减文章刷新数据库一下页面才会改变,因为它保存在了数据库表wp_options下SHe_archives_***这个option_name中,如果把年放在前面,那么%s %d也要相应调换一下位置*/
function archives_list_SHe() {
     global $wpdb,$month;
     $lastpost = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_date <'" . current_time('mysql') . "' AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC LIMIT 1");
     $output = get_option('SHe_archives_'.$lastpost);
     if(empty($output)){
         $output = '';
         $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'SHe_archives_%'");
         $q = "SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM $wpdb->posts p WHERE post_date <'" . current_time('mysql') . "' AND post_status='publish' AND post_type='post' AND post_password='' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
         $monthresults = $wpdb->get_results($q);
         if ($monthresults) {
             foreach ($monthresults as $monthresult) {
             $thismonth    = zeroise($monthresult->month, 2);
             $thisyear    = $monthresult->year;
             $q = "SELECT ID, post_date, post_title, comment_count FROM $wpdb->posts p WHERE post_date LIKE '$thisyear-$thismonth-%' AND post_date AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC";
             $postresults = $wpdb->get_results($q);
             if ($postresults) {
                 $text = sprintf( '%d年 %s', $monthresult->year, $month[zeroise($monthresult->month,2)] );
                 $postcount = count($postresults);
                 $output .= '<ul class="archives-list"><li><span class="archives-yearmonth">' . $text . '  (' . count($postresults) . ' 篇文章)</span><ul class="archives-monthlisting">' . "\n";
             foreach ($postresults as $postresult) {
                 if ($postresult->post_date != '0000-00-00 00:00:00') {
                 $url = get_permalink($postresult->ID);
                 $arc_title    = $postresult->post_title;
                 if ($arc_title)
                     $text = wptexturize(strip_tags($arc_title));
                 else
                     $text = $postresult->ID;
                     $title_text = wp_specialchars($text, 1);
                     $output .= '<li>' . mysql2date('d日', $postresult->post_date) . ': ' . "<a rel='bookmark' href='$url' title='$title_text'>$text</a>";
                     $output .= ' (' . $postresult->comment_count . ')';
                     $output .= '</li>' . "\n";
                 }
                 }
             }
             $output .= '</ul></li></ul>' . "\n";
             }
         update_option('SHe_archives_'.$lastpost,$output);
         }else{
             $output = '<div class="errorbox">Sorry, no posts matched your criteria.</div>' . "\n";
         }
     }
     echo $output;
 }

二、复制一份主题的page.php更名为sitemap.php,然后在最顶端加入如下代码:

<?php
/*
Template Name: SiteMap
*/
?>

三、找到类似 php the_content,在其下面加入如下代码

<div class="sitemap">
	<h3><a id="expand_collapse" href="#">全部展开/收缩</a></h3>
	<div id="archives"><?php archives_list_SHe(); ?></div>
</div>

四、加载jQuery库,并把以下js代码放入sitemap.php

<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function($){
/* 存档页面 jQ伸缩 */
$('#expand_collapse,.archives-yearmonth').css({cursor:"s-resize"});
$('#archives ul li ul.archives-monthlisting').hide();
$('#archives ul li ul.archives-monthlisting:first').show();
$('#archives ul li span.archives-yearmonth').click(function(){$(this).next().slideToggle('fast');return false;});
//以下是全局的操作
$('#expand_collapse').toggle(
function(){
$('#archives ul li ul.archives-monthlisting').slideDown('fast');
},
function(){
$('#archives ul li ul.archives-monthlisting').slideUp('fast');
});
});
//]]>
</script>

五、添加CSS,以YLife主题为例,在style.css文件中添加如下代码:

/*SiteMap_Start*/
.sitemap h3 {color:#565656;margin:10px 0px 12px 0px;font-size:15px;}
.sitemap li:before {content:none; /*去掉上面在li列表前添加东西*/}
.sitemap .archives-list {margin:4px 0px 4px 12px;}
.sitemap .archives-list .archives-monthlisting li {margin:4px 0px 4px 29px;}
/*SiteMap_End*/

六、新建页面,模板选择“SiteMap”即可。

本文标签: ,
本文链接: add-sitemap-archives-page/
版权所有: 玻璃泉, 转载请注明本文出处。

4个评论

  1. 我都懒的建这个地图了!

  2. 没有简单一点的办法么 :!: