【问题标题】:Display content on first post of the month within a blogroll- Wordpress在 blogroll-Wordpress 中每月的第一篇文章中显示内容
【发布时间】:2013-07-10 12:34:30
【问题描述】:

我想要做的是创建一个时间线。我想要发生的事情是在每个月的第一个帖子的顶部显示日期。所以它就像一个分隔符,现在知道下面的帖子是在这个月。

目前我只是查询一个类别中的所有帖子。现在我想要发生的是一个 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; ?>

【问题讨论】:

    标签: php wordpress loops foreach codex


    【解决方案1】:
    <?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; ?>
    

    【讨论】:

    • 对不起,我在第一次完成之前不小心发布了答案,所以我不得不编辑并完成;)
    • 解决这个问题的一个小方法是得到一个 WP_Query 错误 $prev_month = -1;foreach( $myposts as $post ): ?>