php - Wordpress 时间线每季度添加标题 1 月至 3 月等等

标签 php css wordpress

有什么方法可以让我在 1 月至 3 月的顶部显示该标题下的 show posts January to March?然后是四月至六月、七月至九月、十月至十二月。这是我的页面链接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>

关于php - Wordpress 时间线每季度添加标题 1 月至 3 月等等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776730/

相关文章:

html - 偶尔的背景图像故障

php - jQuery ajax 不会将 post 变量发送到 WordPress

xml - 如果你有字符串翻译,wpml-config.xml 有什么用?

mysql - 无法连接到 AWS 上的 mysql RDS 实例

php - 无法将 PHP 变量传递给 Javascript 函数

javascript - 在下拉列表更改事件上更新数据库

javascript - 使用 css3 列 - 如何获得元素位置?

css - 如何在网页的右侧和左侧对齐页脚文本?

php - 脚本向 php 中的注册用户发送自动生日祝福时出错

php - Yii 按关系列 HAS_MANY 排序