2011-08-31 58 views
1

我想創建一個循環,從任何帖子加載一個隨機圖像,同時也檢索特定頁面的摘錄。我做了隨機的帖子部分,但無法獲取它的頁面摘錄...我想我可能需要查詢自己的循環中的頁面,但我不知道如何做到這一點。我已經安裝了獲取頁面摘錄支持等功能,但我認爲我在循環內做了一些錯誤,任何幫助將不勝感激。獲取循環內的特定頁面的摘錄Wordpress

<div class="postimage"> 
<?php if (have_posts()) : 
query_posts('showposts=1&orderby=rand'); 
while (have_posts()) : the_post(); ?> 
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('blog-post-image'); ?></a> 
    <div class="borderimage"></div> 
    <div class="tagline"><h1><?php the_excerpt('$page_id=8'); ?> </h1> 
</div> 
    </div> 
</div> 
<?php endwhile; else : endif; ?> 
+0

嘗試'the_excerpt()'(不帶參數)。另外,你應該把query_posts語句之前'如果(have_posts())' –

+0

我需要parameteres因爲我試圖從特定的頁面上摘錄摘錄,同時從隨機帖子中繪製圖像。 – Amy

+0

啊。得到它了。請參閱下面的答案。 –

回答

0

query_posts取代全球$wp_query,你既然你想保住你的查詢您的網頁不想做的事。試試這個...

if (have_posts()){ 
    while(have_posts()){ 
     the_post(); //global $post now has the page in it 
     $args = array("posts_per_page"=>1,"orderby"=>"rand"); 
     $random_posts = get_posts($args); //returns an array 
     $random_post = $random_posts[0]; 
     //do your stuff... 
     //$post contains the original page 
     //$random_post contains the random post 
    } 
}