Reputation: 69
I've found this script nesting months under years in a wordpress archive dropdown list.
<div class="blog-list-archive">
<?php
/**/
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date)
FROM $wpdb->posts WHERE post_status = 'publish'
AND post_type = 'post' ORDER BY post_date DESC");
foreach($years as $year) :
?>
<li><a href="JavaScript:void()"><?php echo $year; ?></a>
<ul class="archive-sub-menu">
<? $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date)
FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'
AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
foreach($months as $month) :
?>
<li><a href="<?php echo get_month_link($year, $month); ?>">
<?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
</li>
<?php endforeach;?>
</ul>
The output is this :
2014
January
October
2013
January
The script is limited and doesn't show the post under that month. I have tried to add it but no luck, i haven't coded for ages and can't figure out the solution. Other than showing posts i've wanted to add a post counter on year ( shows total number of posts on that year ) and month ( shows total number of posts on that month )
The jquery script to show the dropdown list
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('.blog-list-archive li ul').hide();
jQuery('.blog-list-archive li a').click(function(){
jQuery(this).parent().addClass('selected');
jQuery(this).parent().children('ul').slideDown(250);
jQuery(this).parent().siblings().children('ul').slideUp(250);
jQuery(this).parent().siblings().removeClass('selected');
});
});
</script>
Upvotes: 0
Views: 1396
Reputation: 1383
As Pieter Goosen suggested multi dimensional array will be the right choice.
Its should look like this.
function get_posts_archive_order_date() {
global $wpdb;
$results = $wpdb->get_results("SELECT ID, post_date, post_title FROM {$wpdb->prefix}posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC");
$archiveArr = array();
foreach($results as $result) {
$year = date('Y', strtotime($result->post_date)); // get post year
$month = date('m', strtotime($result->post_date)); // get post month
$archiveArr[$year][$month][$result->ID] = $result; // set the array
}
foreach($archiveArr as $year=>$months) {
$total_year = 0;
foreach($months as $month=>$posts) {
$total_year = $total_year + count($posts); // set the total posts for this year
}
?>
<li><a href="JavaScript:void()"><?php echo $year; ?></a><?php echo $total_year; // print total posts for this year ?>
<ul class="archive-sub-menu">
<?php foreach($months as $month=>$posts) { ?>
<?php $total_month = count($posts); //total posts in this month ?>
<li><a href="<?php echo get_month_link($year, $month); ?>">
<?php echo date( 'F', mktime(0, 0, 0, $month) ); ?></a> <?php echo $total_month; // print total posts for this month ?>
<ul class="archive-sub-menu-posts">
<?php foreach($posts as $post) { ?>
<li>
<a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a>
</li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
</li>
<?php
}
}
get_posts_archive_order_date() // run the function
Upvotes: 1