2010-04-05 109 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