【发布时间】:2014-10-21 08:09:56
【问题描述】:
我发现这个脚本在 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>
【问题讨论】:
-
这确实是一个有趣但也是一个巨大的问题。我的想法是使用单个
WP_Query一次性获取所有帖子,然后获取$posts帖子数组,将其重新排列为以年和月为键的多维数组。然后可以使用这个重新排序的数组来显示您的列表 -
我认为
get_posts可能是更好的选择。