TeddyCoder | Web Freelancer in Hochiminh City

Archive date

<?php wp_get_archives('type=monthly&post_type=blog'); ?>

Other 1:

<ul class="lstCate_ob_date">
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date)
FROM $wpdb->posts WHERE post_status = 'publish'
AND post_type = 'observation' ORDER BY post_date DESC");
foreach($years as $year) :
$years_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'observation' AND YEAR(post_date) = ".$year.";" ) );
?>
<li>
<p class="slideToggle"><?php echo $year; ?>年<?php echo ' ('.$years_count.')'; ?></p>
<ul class="lstCate_ob_sub">
<?php
$months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date)
FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'observation'
AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
foreach($months as $month) :
$months_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'observation' AND YEAR(post_date) = ".$year." and MONTH(post_date) = ".$month.";" ) );
?>
<li><a href="<?php echo get_month_link($year, $month); ?>?post_type=observation">
<?php echo $month.'月'; ?><?php echo ' ('.$months_count.')'; ?></a>
</li>
<?php endforeach;?>
</ul>
</li>
<?php endforeach; ?>
</ul>

 

Other 2: by User

<ul class="lstArchi">
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date)
FROM $wpdb->posts WHERE post_status = 'publish' AND (wp_posts.post_author = ".$curauth->ID.")
AND post_type = 'blog' ORDER BY post_date DESC");
foreach($years as $year) :
$years_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND (wp_posts.post_author = ".$curauth->ID." ) AND post_type = 'blog' AND YEAR(post_date) = ".$year.";" ) );
?>
<li id="Y_<?php echo $year; ?>">
<p class="slideToggle"><?php echo $year; ?>年<?php echo ' ('.$years_count.')'; ?></p>
<ul class="lstCate_ob_sub li_<?php echo $year; ?>">
<?php
$months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date)
FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'blog' AND (wp_posts.post_author = ".$curauth->ID." )
AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
foreach($months as $month) :
$months_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND (wp_posts.post_author = ".$curauth->ID." ) AND post_type = 'blog' AND YEAR(post_date) = ".$year." and MONTH(post_date) = ".$month.";" ) );
?>
<li><a href="<?php echo get_month_link($year, $month); ?>?post_type=blog&a=<?php echo $curauth->ID; ?>">
<?php echo $month.'月'; ?><?php echo ' ('.$months_count.')'; ?></a>
</li>
<?php endforeach;?>
</ul>
</li>
<?php endforeach; ?>
</ul>

 

Other 3 :by function

<?
$monthly_post_obj = archive_year_month('staff_blog');
if($monthly_post_obj){
?>

<ul class="cate_archive">
<?
foreach($monthly_post_obj as $monthly_post_obj){
?>
<li>
<a href="<? echo APP_URL_WP.'/'.$monthly_post_obj->year.'/'.$monthly_post_obj->month.'/?post_type=staff_blog';?>"><? echo $monthly_post_obj->year.' 年 '.$monthly_post_obj->month. ' 月 (' . $monthly_post_obj->post_count . ')';?></a>
</li>
<?
}
?>
</ul>

<?
}
?>

function archive_year_month($post_type){
global $wpdb;
$limit = 0;
$year_prev = null;
$post_type1 = mysql_real_escape_string($post_type);
$querystr = "SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count
FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = '".$post_type1."'
GROUP BY month , year
ORDER BY post_date DESC";

$months = $wpdb->get_results($querystr);
return $months;
}

 

 

 

 

 

BACK