每个 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 = '

'. "\n";
if ( 1 == $atts['usejs'] ) $html .= '展开所有月份'."\n\n";
$html .= '
    ' . "\n";
    $firstmonth = TRUE;
    foreach( $posts as $yearmonth => $posts ) {
    list( $year, $month ) = explode( '.', $yearmonth );
    $firstpost = TRUE;
    foreach( $posts as $post ) {
    if ( TRUE == $firstpost ) {
    $spchar = $firstmonth ? '-' : '+';
    $html .= '

  • '.$spchar.' ' . sprintf( __('%1$s %2$d'), $wp_locale->get_month($month), $year );
    if ( '0' != $atts['postcount'] )
    {
    $html .= ' (共' . count($posts) . '篇文章)';
    }
    if ($firstmonth == FALSE) {
    $html .= "
    \n
  • \n";
    }
    $html .= "\n

\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 代码如下:,

当前位置: 首页 > 归档




文章归档



目前共有文章: PostCount();?>篇


PostList();?>





,

每个 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 = '

'. "\n";
if ( 1 == $atts['usejs'] ) $html .= '展开所有月份'."\n\n";
$html .= '
    ' . "\n";
    $firstmonth = TRUE;
    foreach( $posts as $yearmonth => $posts ) {
    list( $year, $month ) = explode( '.', $yearmonth );
    $firstpost = TRUE;
    foreach( $posts as $post ) {
    if ( TRUE == $firstpost ) {
    $spchar = $firstmonth ? '-' : '+';
    $html .= '

  • '.$spchar.' ' . sprintf( __('%1$s %2$d'), $wp_locale->get_month($month), $year );
    if ( '0' != $atts['postcount'] )
    {
    $html .= ' (共' . count($posts) . '篇文章)';
    }
    if ($firstmonth == FALSE) {
    $html .= "
    \n
  • \n";
    }
    $html .= "\n

\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 代码如下:

当前位置: 首页 > 归档



文章归档



目前共有文章: PostCount();?>篇


PostList();?>





,

每个 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 = '

'. "\n";
if ( 1 == $atts['usejs'] ) $html .= '展开所有月份'."\n\n";
$html .= '
    ' . "\n";
    $firstmonth = TRUE;
    foreach( $posts as $yearmonth => $posts ) {
    list( $year, $month ) = explode( '.', $yearmonth );
    $firstpost = TRUE;
    foreach( $posts as $post ) {
    if ( TRUE == $firstpost ) {
    $spchar = $firstmonth ? '-' : '+';
    $html .= '

  • '.$spchar.' ' . sprintf( __('%1$s %2$d'), $wp_locale->get_month($month), $year );
    if ( '0' != $atts['postcount'] )
    {
    $html .= ' (共' . count($posts) . '篇文章)';
    }
    if ($firstmonth == FALSE) {
    $html .= "
    \n
  • \n";
    }
    $html .= "\n

\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 代码如下:

当前位置: 首页 > 归档



文章归档



目前共有文章: PostCount();?>篇


PostList();?>