質問

What i am trying to do is create a timeline. What i want to happen is have the date echo in at the top of the first post of each month. So it will act like a separator to know now that the posts below are in this month.

Currently i am just querying all the posts in a category. Now what i want to happen is an if else statement. If its the first post of the the month, echo some text.

 <?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; ?>
役に立ちましたか?

解決

<?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; ?>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top