wordpress日志归档代码解析
每个 wordpress 独立博客基本上都有一个日志归档的功能,主要是记录日志整理的一个档案,又类似一个小的站点导向,某一年以及每一个月发布了多少篇的日志,都可以清楚的看出来,我们来看一下日志归档的功能代码:
//日志归档
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 = '<div class="car-container';
if ( 1 == $atts['usejs'] ) $html .= ' car-collapse';
$html .= '">'. "\n";
if ( 1 == $atts['usejs'] ) $html .= '<a href="#" class="car-toggler">展开所有月份'."</a>\n\n";
$html .= '<ul class="car-list">' . "\n";
$firstmonth = TRUE;
foreach( $posts as $yearmonth => $posts ) {
list( $year, $month ) = explode( '.', $yearmonth );
$firstpost = TRUE;
foreach( $posts as $post ) {
if ( TRUE == $firstpost ) {
$spchar = $firstmonth ? '<span class="car-toggle-icon car-minus">-</span>' : '<span class="car-toggle-icon car-plus">+</span>';
$html .= ' <li><span class="car-yearmonth" style="cursor:pointer;">'.$spchar.' ' . sprintf( __('%1$s %2$d'), $wp_locale->get_month($month), $year );
if ( '0' != $atts['postcount'] )
{
$html .= ' <span title="文章数量">(共' . count($posts) . '篇文章)</span>';
}
if ($firstmonth == FALSE) {
$html .= "</span>\n <ul class='car-monthlisting' style='display:none;'>\n";
} else {
$html .= "</span>\n <ul class='car-monthlisting'>\n";
}
$firstpost = FALSE;
$firstmonth = FALSE;
}
$html .= ' <li>' . mysql2date( 'd', $post->post_date ) . '日: <a target="_blank" href="' . get_permalink( $post->ID ) . '">' . 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="评论数量">(' . $post->comment_count . '条评论)</span>';
$html .= "</li>\n";
}
$html .= " </ul>\n </li>\n";
}
$html .= "</ul>\n</div>\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();
将本代码放入到主题 functions.php 里面,即可实现站点归档的功能,在前台显示时我们还需要新建一个页面模板,模板选择 Archives,别名为 Archives,实例如 https://www.xiariboke.net/Archives 所示,如果模板中没有显示页面的选项,可以自己在合适的地方做个链接。
模板 Archives.php 代码如下:
<div id="mapsite">当前位置: <a title="返回首页" href="<?php echo get_settings('Home'); ?>/">首页</a> > 归档</div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="left left_page">
<h2>文章归档</h2>
<div class="article article_page">
<p class="articles_all"><strong><?php bloginfo('name'); ?></strong> 目前共有文章: <?php echo $hacklog_archives->PostCount();?>篇 </p>
<?php echo $hacklog_archives->PostList();?>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
</div>