Php 在wordpress存档下拉列表中以嵌套的月份显示post和counter
我在wordpress存档下拉列表中发现这个脚本嵌套了数月到数年Php 在wordpress存档下拉列表中以嵌套的月份显示post和counter,php,mysql,sql,wordpress,Php,Mysql,Sql,Wordpress,我在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"); for
<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>
脚本是有限的,不显示该月下的帖子。我试图添加它,但没有运气,我已经很久没有编码了,也无法找到解决方案。除了显示帖子之外,我还想在年份(显示当年的帖子总数)和月份(显示当月的帖子总数)添加一个帖子计数器
显示下拉列表的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>
jQuery(文档).ready(函数(){
jQuery('.blog list archive li ul').hide();
jQuery('.blog list archive li a')。单击(函数(){
jQuery(this.parent().addClass('selected');
jQuery(this).parent().children('ul').slideDown(250);
jQuery(this).parent().sibbins().children('ul').slideUp(250);
jQuery(this).parent().sibbins().removeClass('selected');
});
});
正如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
函数get\u posts\u archive\u order\u date(){
全球$wpdb;
$results=$wpdb->get_results(“从{$wpdb->prefix}帖子中选择ID、post_日期、post_标题,其中post_类型='post'和post_状态='publish'按post_日期描述排序”);
$archiveArr=array();
foreach($results作为$result){
$year=date('Y',strottime($result->post_date));//获取post year
$month=date('m',strottime($result->post_date));//获取post month
$archiveArr[$year][$month][$result->ID]=$result;//设置数组
}
foreach($archiveArr作为$year=>$months){
$total_year=0;
foreach($month as$month=>$posts){
$total_year=$total_year+count($posts);//设置今年的总职位
}
?>
-
-
这确实是一个有趣但也是一个巨大的问题。我的想法是通过一个WP\u查询
一次性获取所有帖子,然后将$posts
帖子数组重新排列成以年和月为键的多维数组。这个重新排序的数组可以用来显示您的列表我认为de>get_posts
可能是一个更好的选择。你应该使用get_posts
。根据编码标准,不应该使用自定义SQL查询。我没有测试你的代码,但是+1是为了让我的想法和它一起运行,这正是我的想法:-)真棒@Shibi!非常有魅力。现在我的问题是counter表示年和月在这2条上的帖子数量。我查看了$archiveArr,因为它存储了所有条目,并且可能在上面设置计数器。但我发现很难实现,或者以其他方式在年和月下的foreach循环上设置计数器。-Jan BantolinayI编辑代码,我添加了年和月的计数器。
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