dqx13503925528 2014-10-21 08:09
浏览 83

在wordpress存档下拉列表中显示在几年内的嵌套月份中的帖子和计数器

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>
  • 写回答

1条回答 默认 最新

  • duanba3707 2014-10-21 10:51
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致