2012-03-04 108 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; 
?>