根据Wordpress中帖子的时间戳显示“新”图像

时间:2013-09-26 18:47:15

标签: wordpress if-statement

如果符合以下条件,我想在帖子旁边显示“NEW”图片:

  1. 发表于1至45天之前。
  2. 45天后隐藏/停用“新”图片。
  3. 这是我的旋转木马的代码,显示最新的4个帖子:

    `<!------Begin Carousel Cusomizations-------->
    <div class="list_carousel">
    <a id="prev2" class="prev" href="#">&lt;</a>
    <ul id="foo2">
    <?php
    global $post;
    $args = array( 'numberposts' => -1, 'post_type' => 'guides');
        $posts = get_posts($args);
        foreach( $posts as $post ) : setup_postdata($post); ?>
    <li class="productCarousel" onclick="document.location.href='<?php echo the_permalink(); ?>';">
    <div class="liContent">
    <div class="ribbon"><div class="ribbon-new">New</div></div>
    <div class="liPadding">
    <?php the_post_thumbnail( array(75,75) ); ?>
    <h4><?php the_title(); ?></h4>
    <p><?php the_excerpt(); ?></p>
    <p class="accessGuides"><a href="<?php echo the_permalink(); ?>">Access Guide</a></p>
    </div>
    </div>
    </li>
    <?php endforeach; ?>
    </ul>
    <a id="next2" class="next" href="#">&gt;</a>
    <div class="clearfix"></div>
    <div id="pager2" class="pager"></div>
    </div>    
    </div>
    <!----End Carousel Customizations----->`
    

    DIV部分:<div class="ribbon"><div class="ribbon-new">New</div></div>是我的“新”图片横幅的容器。

    非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

<?php 
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    // posts in the last 45 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-45 days')) . "'";
    return $where;
}
?>
<!------Begin Carousel Cusomizations-------->
<div class="list_carousel">
    <a id="prev2" class="prev" href="#">&lt;</a>
    <ul id="foo2">
    <?php
    global $post;
    $args = array( 'numberposts' => -1, 'post_type' => 'guides');
    add_filter( 'posts_where', 'filter_where' );
    $posts = get_posts($args);
    foreach( $posts as $post ) : setup_postdata($post); ?>
        <li class="productCarousel" onclick="document.location.href='<?php echo the_permalink(); ?>';">
            <div class="liContent">
                <div class="ribbon"><div class="ribbon-new">New</div></div>
                    <div class="liPadding">
                    <?php the_post_thumbnail( array(75,75) ); ?>
                    <h4><?php the_title(); ?></h4>
                    <p><?php the_excerpt(); ?></p>
                    <p class="accessGuides"><a href="<?php echo the_permalink(); ?>">Access Guide</a></p>
                </div>
            </div>
        </li>
    <?php endforeach; 
    remove_filter( 'posts_where', 'filter_where' );
    ?>
    </ul>
    <a id="next2" class="next" href="#">&gt;</a>
    <div class="clearfix"></div>
    <div id="pager2" class="pager"></div>
</div> 
<!----End Carousel Customizations----->`

更新了答案,以便在过去45天内发布帖子时显示“新功能区”文字

<!------Begin Carousel Cusomizations-------->
<div class="list_carousel">
    <a id="prev2" class="prev" href="#">&lt;</a>
    <ul id="foo2">
    <?php
    global $post;
    $args = array( 'numberposts' => -1, 'post_type' => 'guides');
    $posts = get_posts($args);
    foreach( $posts as $post ) : setup_postdata($post); ?>
        <li class="productCarousel" onclick="document.location.href='<?php echo the_permalink(); ?>';">
            <div class="liContent">

                    <?php if( strtotime('-45 days') < strtotime( $post->post_date )  ) {
                        <div class="ribbon">
                            <div class="ribbon-new">New</div>
                        </div>
                    <?php } ?>

                    <div class="liPadding">
                    <?php the_post_thumbnail( array(75,75) ); ?>
                    <h4><?php the_title(); ?></h4>
                    <p><?php the_excerpt(); ?></p>
                    <p class="accessGuides"><a href="<?php echo the_permalink(); ?>">Access Guide</a></p>
                </div>
            </div>
        </li>
    <?php endforeach; ?>
    </ul>
    <a id="next2" class="next" href="#">&gt;</a>
    <div class="clearfix"></div>
    <div id="pager2" class="pager"></div>
</div> 
<!----End Carousel Customizations----->