2011-04-08 35 views
2

我做了一個WordPress的主題,頁和帖子。 帖子循環顯示我的簡短帖子和繼續閱讀鏈接。 我喜歡這個,但是我怎樣才能在開始時發佈的循環圖像(s)的帖子簡要中做主題展示,如果有的話。WordPress的 - 循環與張貼的圖像

謝謝!

回答

0

您應該添加在你的循環:

<?php 
    if(has_post_thumbnail()) { 
     $theimage = wp_get_attachment_image_src(get_post_thumbnail_id ($post->ID), 'thumbnail'); 
} 
?> 
<img class="img_class" src="<?php echo $theimage[0]; ?>" /> 

"thumbnail"對應要顯示的大小。

請記住,也有WordPress specific site in StackExchange

+0

我知道這種方法,但是這隻顯示特色圖片,而不是插入到文章中的圖片。 – 2011-04-08 22:25:41

+0

那麼,你問這樣的事情? [獲取第一張圖片](http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it) – konus 2011-04-08 22:37:12

+2

如果你不使用wordpress的圖片管理員添加圖片發佈後,您將不得不使用該解決方案。但是,如果你真的使用wordpress的圖片管理器,你可能需要使用answeres我的解決方案。因爲,在郵政上做正則表達式並不是真正聰明的解決方案。假設你對你的帖子有興趣並且首先出現,你最終會顯示它。 – ariefbayu 2011-04-08 22:47:04

3

您可以通過使用讓您的圖片附件:

$args = array(
    'post_type' => 'attachment', 
    'post_mime_type' => 'image', 
    'numberposts' => 1, 
    'orderby' => 'menu_order', 
    'order' => 'ASC', 
    'post_parent' => $post->ID 
); 
$images = get_posts($args); 

像這樣顯示出來:

echo wp_get_attachment_image($images[0]->ID, $size='attached-image'); 
2

這對於讓所有attachement圖片與您的帖子。

$args = array( 
       'post_type' => 'attachment', 
       'post_mime_type' => 'image', 
       'post_status' => null, 
       'post_parent' => $post->ID 
      ); 

    $attachments = get_posts($args); 

    if ($attachments) { 
     foreach ($attachments as $post) { 
     $img = wp_get_attachment_image_src($post->ID, 'medium'); 
     $fullsize = wp_get_attachment_image_src($post->ID, 'full'); 
     } 
    }