2013-02-14 73 views
0

我如何过滤WordPress中的帖子或第一张图片,然后显示添加到主题的CSS类。我看到我可以在我的functions.php中使用API​​中的add_filter()函数,但是我有问题需要获取每个帖子的第一张图片。WordPress的帖子第一张图片

<a href="http://localhost/wordpress/wp-content/uploads/2012/10/test.jpg"> 
<img class="alignnone size-full wp-image-95" width="540" height="300" alt="dell-vs-apple" src="http://localhost/wordpress/wp-content/uploads/2012/10/dell-vs-apple1.jpg"> 
</a> 


<div class = "my_class"> 
<a href="http://localhost/wordpress/wp-content/uploads/2012/10/dell-vs-apple1.jpg"> 
<img class="alignnone size-full wp-image-95" width="540" height="300" alt="dell-vs-apple" src="http://localhost/wordpress/wp-content/uploads/2012/10/dell-vs-apple1.jpg"> 
</a> 

回答

0

这是一个方式来获得职位的第一形象..

// Get URL of first image in a post 
    function o99_find_first_post_image() { 
     global $post, $posts; 
     $first_img = ''; 
     ob_start(); 
     ob_end_clean(); 
     $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); 
     $first_img = $matches [1] [0]; 

     // if no image found we can choose to display some default image instead 
     if(empty($first_img)){ 
      $first_img = "/images/default.jpg";// path to default image 
     } 
     return $first_img; 
    } 
相关问题