有没有什么方法可以让我在1月到3月的标题下显示1月到3月的文章?然后是四月六月七月九月十月十二月。这是我的网页链接http://novartis.portlandvault.com/timeline/
这是我的时间线模板代码

<!-- Timeline begin here -->
<div id="timeline">
<?php
    //Define your custom post type name in the arguments
    $args = array('post_type' => 'timeline', 'order' => 'asc');
    //Define the loop based on arguments
    $loop = new WP_Query( $args );
    //Display the contents
    while ( $loop->have_posts() ) : $loop->the_post();
    $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID,'large') );
?>
<section id="<?php the_time( 'M' );?>">
    <div class="timeline-item">
        <?php
            if( $loop->current_post === 0 ) {
        ?>
        <div class="timeline-icon">
            <div class="timeline-month">
                <?php the_time( 'M' );?>
            </div>
        </div>
        <?php $current_month = get_the_time('M'); ?>
        <?php } else {
            $post_month = get_the_time('M');
            if($current_month != $post_month) {
        ?>
        <div class="timeline-icon">
            <div class="timeline-month">
                <?php the_time( 'M' );?>
            </div>
        </div>
        <?php }
            }
            $current_month = get_the_time('M');
        ?>
        <div class="timeline-content right">
            <h2>
                <?php the_title(); ?>
            </h2>
            <p>
                <?php echo the_content(); ?>
            </p>
            <div class="timeline_img">
                <img src="<?php echo $thumb; ?>" class="img-responsive">
            </div>
        </div>
    </div>
</section>
<?php endwhile;?>
</div>
</div><!-- Column End -->
</div><!-- Container End -->

最佳答案

我认为您最好的选择是使用date parameters为每个季度使用一个WP_Query实例。
下面是一个例子:

<!-- Timeline begin here -->
<div id="timeline">
    <?php
    //Define your custom post type name in the arguments
    $args = array('post_type' => 'timeline', 'order' => 'asc');

    $current_year = get_the_time('Y');

    $quarters = array(
        // Q1, January - March
        __( 'January - March' ) => array(
            'after' => array(
                'year' => $current_year,
                'month' => 1,
                'day' => 1,
            ),
            'before' => array(
                'year' => $current_year,
                'month' => 3,
                'day' => 31,
            ),
            /* Set inclusive to true so we capture
            ** posts on the before/after days
             */
            'inclusive' => true,
        ),
        // Q2, April - June
        __( 'April - June' ) => array(
            'after' => array(
                'year' => $current_year,
                'month' => 4,
                'day' => 1,
            ),
            'before' => array(
                'year' => $current_year,
                'month' => 6,
                'day' => 30,
            ),
            'inclusive' => true,
        ),
        // Q3, July - September
        __( 'July - September' ) => array(
            'after' => array(
                'year' => $current_year,
                'month' => 7,
                'day' => 1,
            ),
            'before' => array(
                'year' => $current_year,
                'month' => 9,
                'day' => 30,
            ),
            'inclusive' => true,
        ),
        // Q4, October - December
        __( 'October - December' ) => array(
            'after' => array(
                'year' => $current_year,
                'month' => 10,
                'day' => 1,
            ),
            'before' => array(
                'year' => $current_year,
                'month' => 12,
                'day' => 31,
            ),
            'inclusive' => true,
        ),
    );

    foreach ( $quarters as $quarter_heading => $quarter_date_query ):

        // set current quarter args to our default args
        $quarter_args = $args;

        // add the date query for this quarter
        $quarter_args['date_query'] = $quarter_date_query;

        // display the heading for the current quarter
        ?>

        <div class="quarterlyheading">
            <?php echo  $quarter_heading; ?>
            <div class="quarterlinebreak"><hr></div>
        </div>

        <?php
        // loop through the posts for the current quarter

        //Define the loop based on arguments
        $loop = new WP_Query( $quarter_args );

        //Display the contents
        while ( $loop->have_posts() ) : $loop->the_post();
            $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID,'large') );
            ?>
            <section id="<?php the_time( 'M' );?>">
                <div class="timeline-item">
                    <?php
                    if( $loop->current_post === 0 ) {
                        ?>
                        <div class="timeline-icon">
                            <div class="timeline-month">
                                <?php the_time( 'M' );?>
                            </div>
                        </div>
                        <?php $current_month = get_the_time('M'); ?>
                    <?php } else {
                        $post_month = get_the_time('M');
                        if($current_month != $post_month) {
                            ?>
                            <div class="timeline-icon">
                                <div class="timeline-month">
                                    <?php the_time( 'M' );?>
                                </div>
                            </div>
                        <?php }
                    }
                    $current_month = get_the_time('M');
                    ?>
                    <div class="timeline-content right">
                        <h2>
                            <?php the_title(); ?>
                        </h2>
                        <p>
                            <?php echo the_content(); ?>
                        </p>
                        <div class="timeline_img">
                            <img src="<?php echo $thumb; ?>" class="img-responsive">
                        </div>
                    </div>
                </div>
            </section>
        <?php
        endwhile;

        // reset post data
        wp_reset_postdata();

    endforeach;

?>
</div>

10-06 12:54