wordpress “歸檔”頁(yè)面,
特點(diǎn):
01.訪問(wèn)速度快。
02.顯示每月文章數(shù)。
03.顯示每篇文章的評(píng)論數(shù)。
效果:見我網(wǎng)站的
存檔頁(yè)下面是詳細(xì)步驟
1. 把下面的函數(shù)代碼扔進(jìn)主題的 functions.php 里面 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class hacklog_archives
{
function GetPosts()
{
global $wpdb;
if ( $posts = wp_cache_get( 'posts', 'ihacklog-clean-archives' ) )
return $posts;
$query="SELECT DISTINCT ID,post_date,post_date_gmt,comment_count,comment_status,post_password FROM $wpdb->posts WHERE post_type='post' AND post_status = 'publish' AND comment_status = 'open'";
$rawposts =$wpdb->get_results( $query, OBJECT );
foreach( $rawposts as $key => $post ) {
$posts[ mysql2date( 'Y.m', $post->post_date ) ][] = $post;
$rawposts[$key] = null;
}
$rawposts = null;
wp_cache_set( 'posts', $posts, 'ihacklog-clean-archives' );;
return $posts;
}
function PostList( $atts = array() )
{
global $wp_locale;
global $hacklog_clean_archives_config;
$atts = shortcode_atts(array(
'usejs' => $hacklog_clean_archives_config['usejs'],
'monthorder' => $hacklog_clean_archives_config['monthorder'],
'postorder' => $hacklog_clean_archives_config['postorder'],
'postcount' => '1',
'commentcount' => '1',
), $atts);
$atts=array_merge(array('usejs'=>1,'monthorder' =>'new','postorder' =>'new'),$atts);
$posts = $this->GetPosts();
( 'new' == $atts['monthorder'] ) ? krsort( $posts ) : ksort( $posts );
foreach( $posts as $key => $month ) {
$sorter = array();
foreach ( $month as $post )
$sorter[] = $post->post_date_gmt;
$sortorder = ( 'new' == $atts['postorder'] ) ? SORT_DESC : SORT_ASC;
array_multisort( $sorter, $sortorder, $month );
$posts[$key] = $month;
unset($month);
}
$html = '</pre>
<div class="car-container'; if ( 1 == $atts['usejs'] ) $html .= ' car-collapse'; $html .= '">'. "\n";
if ( 1 == $atts['usejs'] ) $html .= '<a class="car-toggler" href="#">展開所有月份'."</a>\n\n";
$html .= '
<ul class="car-list">
<ul class="car-list">' . "\n";</ul>
</ul>
<ul class="car-list">
<ul class="car-list">$firstmonth = TRUE;</ul>
</ul>
<ul class="car-list">
<ul class="car-list">foreach( $posts as $yearmonth => $posts ) {</ul>
</ul>
<ul class="car-list">
<ul class="car-list">list( $year, $month ) = explode( '.', $yearmonth );</ul>
</ul>
<ul class="car-list">
<ul class="car-list">$firstpost = TRUE;</ul>
</ul>
<ul class="car-list">
<ul class="car-list">foreach( $posts as $post ) {</ul>
</ul>
<ul class="car-list">
<ul class="car-list">if ( TRUE == $firstpost ) {</ul>
</ul>
<ul class="car-list">
<ul class="car-list">
<ul class="car-list">$html .= '</ul>
</ul>
</ul>
<ul class="car-list">
<ul class="car-list">
<li><span class="car-yearmonth">+ ' . sprintf( __('%1$s %2$d'), $wp_locale->get_month($month), $year );
if ( '0' != $atts['postcount'] )
{
$html .= ' <span title="文章數(shù)量">(共' . count($posts) . '篇文章)</span>';
}
$html .= "</span>\n
<ul class="car-monthlisting">
<ul class="car-monthlisting">\n";</ul>
</ul>
<ul class="car-monthlisting">
<ul class="car-monthlisting">$firstpost = FALSE;</ul>
</ul>
<ul class="car-monthlisting">
<ul class="car-monthlisting">}</ul>
</ul>
<ul class="car-monthlisting">
<ul class="car-monthlisting">
<ul class="car-monthlisting">$html .= '</ul>
</ul>
</ul>
<ul class="car-monthlisting">
<ul class="car-monthlisting">
<li>' . mysql2date( 'd', $post->post_date ) . '日: <a href="' . get_permalink( $post->ID ) . '" target="_blank">' . get_the_title( $post->ID ) . '</a>';
if ( '0' != $atts['commentcount'] && ( 0 != $post->comment_count || 'closed' != $post->comment_status ) && empty($post->post_password) )
$html .= ' <span title="評(píng)論數(shù)量">(' . $post->comment_count . '條評(píng)論)</span>';
$html .= "</li>
</ul>
</ul>
<ul class="car-monthlisting">
<ul class="car-monthlisting">\n";</ul>
</ul>
<ul class="car-monthlisting">
<ul class="car-monthlisting">}</ul>
</ul>
<ul class="car-monthlisting">$html .= "</ul>
\n</li>
</ul>
</ul>
<ul class="car-list">
<ul class="car-list">\n";</ul>
</ul>
<ul class="car-list">
<ul class="car-list">}</ul>
</ul>
<ul class="car-list">$html .= "</ul>
\n</div>
<pre>\n";
return $html;
}
function PostCount()
{
$num_posts = wp_count_posts( 'post' );
return number_format_i18n( $num_posts->publish );
}
}
if(!empty($post->post_content))
{
$all_config=explode(';',$post->post_content);
foreach($all_config as $item)
{
$temp=explode('=',$item);
$hacklog_clean_archives_config[trim($temp[0])]=htmlspecialchars(strip_tags(trim($temp[1])));
}
}
else
{
$hacklog_clean_archives_config=array('usejs'=>1,'monthorder' =>'new','postorder' =>'new');
}
$hacklog_archives=new hacklog_archives();
2. 復(fù)制一份主題的 page.php 更名為 archives.php,然后在最頂端加入:
1
<!--?php /* Template Name: archives */ ?-->
在<?php get_header();?>下面,加入如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<script type="text/javascript">
/* <![CDATA[ */
jQuery(document).ready(function() {
jQuery('.car-collapse').find('.car-monthlisting').hide();
jQuery('.car-collapse').find('.car-monthlisting:first').show();
jQuery('.car-collapse').find('.car-yearmonth').click(function() {
jQuery(this).next('ul').slideToggle('fast');
});
jQuery('.car-collapse').find('.car-toggler').click(function() {
if ( '展開所有月份' == jQuery(this).text() ) {
jQuery(this).parent('.car-container').find('.car-monthlisting').show();
jQuery(this).text('折疊所有月份');
}
else {
jQuery(this).parent('.car-container').find('.car-monthlisting').hide();
jQuery(this).text('展開所有月份');
}
return false;
});
});
/* ]]> */
</script>
再然后找到類似 <?php content(); ?>,在其下面加入如下代碼:
1
<!--?php echo $hacklog_archives--->PostList();?>
3.進(jìn)wp后臺(tái)添加一新頁(yè)面,在右側(cè)欄模板選擇 archives。
剩下的就是對(duì)CSS進(jìn)行調(diào)整以達(dá)到你想要的效果,比如字體大小,顏色等等。
好了,弄完,收工。