在blogroll- Wordpress中显示该月第一篇文章的内容

时间:2013-07-10 12:34:31

标签: php wordpress loops foreach codex

我要做的是创建一个时间轴。我想要发生的是将日期回显放在每月第一篇文章的顶部。所以它现在就像一个分隔符,现在知道下面的帖子是在这个月。

目前我只是在查询某个类别中的所有帖子。现在我想要发生的是if else声明。如果是本月的第一篇文章,请回复一些文字。

 <?php $cat=get_field( 'time_taxonomy');?>
                <?php global $post; $i=0; $args=array( 'numberposts'=>-1, 'offset'=> 0, 'category' => $cat ); $myposts = get_posts( $args ); foreach( $myposts as $post ) : if ($i==1) { } else {}; setup_postdata($post); $i++; ?>
                <div class="ss-container">
                <div class="none"><?php echo date("F"); ?></div>
                    <div class="ss-row">
                        <div class="ss-left">
                             <h2 id="month"><?php echo date("F"); ?></h2>
                        </div>
                        <div class="ss-right">
                             <h2><?php echo date("Y"); ?></h2>
                        </div>
                    </div>
                    <div class="ss-row ss-medium">
                        <!-- Pulling in the featured post imgae and title-->
                        <?php $image=wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'timeline' ); ?>
                        <div class="ss-left">
                            <a href="<?php the_permalink(); ?>" class="ss-circle">
                                <img class="ss-circle-1" src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
                            </a>
                        </div>
                        <div class="ss-right">
                             <h3>
                                <span><?php echo date("F j, Y | g:i a"); ?></span>
                                <a href="#">
                                    <?php the_title(); ?>
                                </a>
                            </h3>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>

1 个答案:

答案 0 :(得分:0)

<?php $cat=get_field( 'time_taxonomy');?>
                <?php global $post; $i=0; $args=array( 'numberposts'=>-1, 'offset'=> 0, 'category' => $cat ); $myposts = get_posts( $args ); $prev_month = -1; foreach( $myposts as $post ) : 
                    $post_month = date("m",strtotime($post["post_date"]));
                    if($post_month != $prev_month) {
                         // First post of the new month
                    } else {
                         // Still the same month
                    }
                    $prev_month = $post_month;
                    setup_postdata($post); $i++; ?>
                <div class="ss-container">
                <div class="none"><?php echo date("F"); ?></div>
                    <div class="ss-row">
                        <div class="ss-left">
                             <h2 id="month"><?php echo date("F"); ?></h2>
                        </div>
                        <div class="ss-right">
                             <h2><?php echo date("Y"); ?></h2>
                        </div>
                    </div>
                    <div class="ss-row ss-medium">
                        <!-- Pulling in the featured post imgae and title-->
                        <?php $image=wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'timeline' ); ?>
                        <div class="ss-left">
                            <a href="<?php the_permalink(); ?>" class="ss-circle">
                                <img class="ss-circle-1" src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
                            </a>
                        </div>
                        <div class="ss-right">
                             <h3>
                                <span><?php echo date("F j, Y | g:i a"); ?></span>
                                <a href="#">
                                    <?php the_title(); ?>
                                </a>
                            </h3>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>