WordPress为最新帖子添加类

时间:2018-10-24 15:10:12

标签: php wordpress class post

有人知道我如何在WordPress中为最新帖子添加类。我最新的意思是在最后一分钟添加的最新帖子?

现在我的代码:

<?php 
    // query
    $the_query = new WP_Query(array(
        'post_type'         => 'post',
        'category_name'     => 'profiel',
        'posts_per_page'    => 48,
        'paged'             => $paged,
        'meta_key'          => 'online',
        'orderby'           => 'meta_value_num',
        'order'             => 'DESC'
    ));
if ( $the_query->have_posts() ) : ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div id="post-<?php echo $post->ID; ?>" class="profiel">
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>

<?php endwhile; ?>

<?php else : ?>

    <div class="404 not-found">
        <h3>Not Found</h3>
        <div class="post-excerpt">
            <p>Sorry, but there are no more posts here... Please try going back to the <a href="<?php echo remove_query_arg( 'pg' ); ?>">main page</a></p>
        </div>
    </div>

<?php endif;

    wp_reset_query();

    the_posts_pagination( array(
        'mid_size' => 10
    ) );

?>

1 个答案:

答案 0 :(得分:1)

获取当前时间,将其删除60秒即可与您的帖子时间戳进行比较。如果您发布的时间戳大于一分钟前的时间戳,则为新时间戳。

<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div id="post-<?php echo $post->ID; ?>" class="profiel <?= (get_the_time('U') > (time()-60)) ? 'new' : '' ?>">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>