根据wordpress帖子的年龄显示图片的条件标签


Conditional tag to display image based on age of wordpress post

您好,我正在尝试根据帖子的年龄输出图像。

例如,对于循环,我想说:如果帖子的日期小于3天,则在帖子旁边显示高级图标,否则,如果帖子的日期大于3天,则在帖子旁边显示常规图标。

事先感谢您的帮助。

<?php global $wp_query; 
query_posts( array(
    'post_type' => 'job_listing' ,
    'showposts' => 5 )
); ?>
<?php if ( have_posts() ): ?>
<div class="shortcode post archive grid two-column">
    <div class="grid-row linearise">
        <div class="grid-item">
            <article class="unboxed">
                <div class="content">
                    <div class="typography">
                    <table style="width:100%">
                        <tr>
                            <th>STATUS</th>
                            <th>JOB TITLE</th>
                            <th>LOCATION</th> 
                            <th>POSTED</th>
                        </tr>
<?php while ( have_posts() ) : the_post(); ?>
                        <tr>
                            <td class="status">
                            <?php if ( $postDate < $todaysDate || $postDate == $todaysDate ) { ?>
                            <img src="<?php bloginfo('template_directory');?>-child/images/icon-premium.png"/>
                            <?php else: ?>
                            <img src="<?php bloginfo('template_directory');?>-child/images/icon-general.png"/>
                            <?php endif; ?>                                     
                            *</td>      
<?php endwhile; ?>
                            <td><a href="<?php esc_url( the_permalink() ); ?>"><?php the_title(); ?></a></td>
                            <td><?php the_job_location( false ); ?></td>
                            <td><span class="datetime"><?php echo get_the_date( "d/m/Y" ); ?></span></td>                                               
                        </tr>   
                    </table>
                    <p>*Premium jobs are jobs between 1-3 days old. You can view all of our other jobs at any time.</p>
                    </div>
                </div>
            </article>
        </div>                                                          
<?php if ( have_posts() ): ?>
    </div>
</div>
<p><a href="<?php echo site_url(); ?>/jobs" target="_self" class="button small">See all of our jobs</a></p>
<?php endif; ?>
<?php else: ?>
<article class="unboxed">
    <div class="content">
        <div class="typography">
            <p>Alternative Content</p>
        </div>
    </div>
</article>
<?php endif; ?> 

尝试使用以下语句获取文章的年龄(以天为单位):

(date('U') - get_post_time('U'))/60/60/24

你可以这样使用:

<?php if ((date('U') - get_post_time('U'))/60/60/24 <= 3): ?>
    <img src="<?php bloginfo('template_directory');?>-child/images/icon-premium.png"/>
<?php else: ?>
    <img src="<?php bloginfo('template_directory');?>-child/images/icon-general.png"/>
<?php endif; ?>