2010-04-05 114 views

回答

2

有一種更好的方式 - 你也可以使用這個功能太 -

function catch_that_image() { 
    global $post, $posts; 
     $first_img = ''; 
     ob_start(); 
     ob_end_clean(); 
     $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); 
     $first_img = preg_replace("/_thumb[0-9]\./", "$1.", $first_img); 

    // no image found display default image instead 
     if(empty($first_img)){ 
      $first_img = "/wp-content/default.png"; 
     } 
     return $first_img; 
} 

如果插入此功能你對你的主題的functions.php可以插入

<img src="<?php echo catch_that_image(); >" width="50" height="50" alt="<?php the_title(); ?>" /> 

您single.php和index.php

此功能將捕獲有史以來第一個圖像,並將顯示它,如果沒有人可用 - 它將使用一個默認圖像,您可以更改...

或者另一種方式:

<?php $image = get_post_meta($post->ID, 'postimage', true); ?> 

<img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" /> 

如果你把這個在您的index.php或single.php中它將使用(在文章/頁面的CustomField)在現場「postimage」給出的圖像。

我希望這有助於

+0

@ahmet:更好,謝謝:) – Sarfraz 2010-04-05 08:13:48

2

最有可能通過採用圖像源的自定義字段。然後,發佈模板將被更改以查看是否設置了圖像,如果是,則包括它。

+0

我也懷疑過,讓我看看是否還有其他選項。謝謝 – Sarfraz 2010-04-05 07:27:27

1

增加對誰通過谷歌找到這個人的附加應答,因爲最初的答案意味着需要大量的手工編碼。

catswhocode博客不再像上面所描述的那樣,所以這個建議可能不太適合,但我認爲值得一提的是WordPress現在明確支持「張貼縮略圖」。欲瞭解更多信息,請參見這裏:http://codex.wordpress.org/Post_Thumbnails

至於文章僅是在頭版的總結,要實現這一種方法是用一個the_excerpt()更換調用the_content(~~~)(例如,在content.php)。有關摘錄的更多信息,請參閱http://codex.wordpress.org/Excerpt