2012-03-04 106 views
0

我想在单个帖子模板循环中有一个条件语句,如果帖子是最近的帖子(即:最新的帖子创建),它会做A如果它不是做B最新帖子if else - wordpress

我到处搜索过,这可能吗?

回答

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 
} 
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; 
?>