查询以显示月份和年份下的所有帖子标题

时间:2014-02-02 12:18:57

标签: php mysql wordpress

我试图通过此订购年/月/标题在帖子的下拉菜单中生成/显示列表。 我找到了以下代码,它生成了年根层次结构,后跟任何有帖子的月份=正是我想要的。我无法将特定月份的实际帖子生成为月份下的列表。如何修改第二个查询以获得最终查询所需的内容?目前最后的查询只是抓住nzca-news-3类别的所有帖子,它需要选择适当月份/年份的帖子。

我很感激任何帮助,因为它有点超出我的意义。 到目前为止,这是我测试的链接。 http://www.thewebsitedeveloper.co.nz/tempProject/nzca-wordpress/news-menu-test/

  <script type="text/javascript">

                $(document).ready(function() {

                    $('.blog-list-archive li ul').hide();
                    $('.blog-list-archive li a').click(function(){
                        $(this).parent().addClass('selected');
                        $(this).parent().children('ul').slideDown(250);                          
                        $(this).parent().siblings().children('ul').slideUp(250);
                        $(this).parent().siblings().removeClass('selected');                          

                    });
                    $('.archive-sub-menu li a').click(function(){
                        $(this).parent().addClass('selected');
                        $(this).parent().children('ul').slideDown(250);                        
                    });
                });
   </script>                   


                <div class="blog-list-archive">
                    <ul class="nzcaNewsList">

                    <?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><img src="<?php echo get_template_directory_uri(); ?>/images/bluePointRightArrow.jpg" alt="" ><a class="title" 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 date( 'F', mktime(0, 0, 0, $month) );?></a>

                                <ul class="archive-posts">
                                    <?php
                                        $cat_id = get_cat_ID('nzca-news-3');

                                        $args=array(
                                          'cat' => $cat_id,                                             
                                          'post_type' => 'post',
                                          'post_status' => 'publish',
                                          'posts_per_page' => -1,
                                          'caller_get_posts'=> 1
                                        );
                                        $my_query = null;
                                        $my_query = new WP_Query($args);
                                        if( $my_query->have_posts() ) {

                                          while ($my_query->have_posts()) : $my_query->the_post(); ?>
                                            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
                                            <?php

                                          endwhile;
                                        }
                                        ?>
                                    <?php
                                    wp_reset_query();
                                    ?>

                                </ul>
                            </li>
                            <?php endforeach;?>
                        </ul>
                        </li>
                    <?php endforeach; ?>
                    </ul>

                </div>

1 个答案:

答案 0 :(得分:0)

以下查询为我提供了按年/月/帖后标题排序的所有帖子的下拉列表,这是我需要的。希望它可以帮助其他人:

<ul class="nzcaNewsList">                           
    <?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><span class="postArrowUp"></span><a class="postYear" href="#"><?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 date( 'F', mktime(0, 0, 0, $month) );?></a>
                                    <ul>
                                            <?
                                            $days = $wpdb->get_col("SELECT ID
                                                            FROM $wpdb->posts
                                                            WHERE post_status = 'publish'
                                                            AND post_type = 'post'
                                                            AND MONTH(post_date) = '".$month."'                                             
                                                            AND YEAR(post_date) = '".$year."'
                                                            ORDER BY post_date DESC");
                                            foreach($days as $day) :
                                            ?>
                                            <li><a href="<?php echo get_permalink( $day ) ?>"><?php echo get_the_title( $day )?> </a></li>
                                            <?php endforeach;?>
                                    </ul>

                            </li>
                            <?php endforeach;?>
                    </ul>
            </li>
        <?php endforeach; ?>                        
    </ul>