if else的最新帖子 - wordpress

时间:2012-03-04 16:34:01

标签: wordpress

我希望在单个帖子模板循环中有一个条件语句,如果帖子是最新帖子(即:最新创建的帖子),如果不是do < A 强>乙

我到处搜索,这可能吗?

2 个答案:

答案 0 :(得分:0)

是的,这是可能的。在主查询之前运行自定义查询以获取最新帖子并比较其ID:

// get 1 most recent post
$query_args = array(
'showposts'     => 1 // here you can add limit by categry etc
);
$query = new WP_Query( $query_args );
$query->the_post();
$recent_post_ID = $post->ID; // this is your most recent post ID

// this is your main loop
if ( have_posts() ) while ( have_posts() ) : the_post(); 

if ( $post->ID == $recent_post_ID ) { // check if IDs are equeal or not
// You are viewing most recent entry
} else {
// You are viewing older entry
}

答案 1 :(得分:0)

您也可以尝试使用WordPress的原生函数来检索最新的帖子

<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
$latest_post_id=$recent_posts[0]['ID'];

if (have_posts()) : while (have_posts()) : the_post(); ?>
    if ($post->ID == $latest_post_id) 
    {
        // code for newest post
    }
    else
    {
        // code for other than the newest post
    }
endwhile;
endif;
?>