2013-02-14 98 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; 
    } 
相關問題