2012-03-04 15 views
0

私は、投稿が最新の投稿(ie:最新の投稿が作成された)であれば、投稿テンプレートループの中に条件文を入れたいと思っています do B最新投稿もしelse - wordpress

どこでも検索しましたが、これは可能ですか?

答えて

0

はい、可能です。メインクエリの前にカスタムクエリを実行すると、最新のポストを取得し、そのIDを比較する:あなたは、最新の投稿を取得するために、WordPressのネイティブ関数であまりにもこれを試して

// 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

<?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; 
?> 
関連する問題