【问题标题】:Display "NEW" image based on timestamp of post in Wordpress在 Wordpress 中根据帖子的时间戳显示“新”图像
【发布时间】:2013-09-26 18:47:15
【问题描述】:

如果出现以下情况,我想在帖子旁边显示“新”图片:

  1. 它是在 1 到 45 天前发布的。
  2. 45 天后隐藏/禁用“新”图像。

这是我的轮播代码,它显示了最新的 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 部分:&lt;div class="ribbon"&gt;&lt;div class="ribbon-new"&gt;New&lt;/div&gt;&lt;/div&gt; 是我的“新”图像横幅的容器。

非常感谢任何帮助!

【问题讨论】:

    标签: wordpress if-statement


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

    【讨论】:

    • 谢谢!但是我会编写什么代码将我包裹在容器 '
      New
      ' 以便它仅在 post_date 为 45 时显示今天的日期是几天或更短?
    • 换句话说,我想在这个轮播中显示所有帖子。但我想在 45 天或更短的帖子上显示这个“新功能区”,以便用户知道这些帖子是最新的。
    • 更新了上面的答案。如果对您有用,请接受答案! (meta.stackexchange.com/a/5235)
    猜你喜欢
    • 2016-01-17
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多