我发现这个脚本在wordpress存档下拉列表中嵌入了几个月。
<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
脚本有限,并且不会显示该月的帖子。我试图添加它,但没有运气,我没有编码多年,无法找出解决方案。除了显示帖子之外,我还希望在年度上添加一个帖子(显示该年度的帖子总数)和月份(显示该月份的帖子总数)
显示下拉列表的jquery脚本
<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>
答案 0 :(得分:1)
正如Pieter Goosen所说,多维数组将是正确的选择。
它应该是这样的。
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