2012-04-19 50 views
0

我試圖用TimThumb到我的博客上顯示圖像PHP:爲什麼URL缺少一個部分?

<?php the_title(); ?>'><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo catch_that_image() ?>&w=290&h=160 

上面的代碼是我用

的問題是圖像不顯示,因爲URL出來像這個。

http://myurl.com/wp-content/themes/ThemeName/timthumb.php?src=&w=290&h=160 

在其被假設是這樣例如

http://myurl.com/wp-content/themes/ThemeName/timthumb.php?src=http://myurl.com/wp-content/uploads/i0n1c.png&w=290&h=160 

正如你可以看到上述缺少部分從src=和缺失部分是這個代碼<?php echo catch_that_image() ?>我分別測試該代碼它工作得很好,但是當我將它們組合在一起時,它不能正確顯示它?

有什麼我要補充,使其作品?

UPDATE:

這裏是我用來抓取圖像

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 = $matches [1] [0]; 

    if(empty($first_img)){ //Defines a default image 
    $first_img = "/images/default.jpg"; 
    } 
    return $first_img; 
} 

這裏是代碼調用該圖像的URL路徑的功能。

<?php echo catch_that_image() ?> 
+1

你試過var_dump($ image)? – Hajo 2012-04-19 22:22:05

+0

@Hajo你能給我更多的提示或信息嗎?我不擅長PHP,因爲我將在今年夏天參加PHP課程。 – Ali 2012-04-19 22:22:54

+0

<?php print_r($ image); ?>插入問題附近的某個地方進行測試,然後查看[0]鍵是否包含您的url丟失的數據,或者是否有另一個或甚至不在那裏。確保從源頭切斷之後它;) – Hajo 2012-04-19 22:25:02

回答

3

我分別測試這些代碼,它工作得很好

該代碼應該輸出變量,讓你在另一種情況下,其中的值設置正確做到了。

更新:

你檢查的生成的源代碼?你有這裏反向:

?src='<?php echo $image[0]; ?>'&w= 
    _      _ 
+0

的事情是,當我只能用這個 Ali 2012-04-19 22:29:05

+0

看到我的更新.. – 2012-04-19 22:43:03

+0

我只是爲了表明我正在使用該代碼。我已經更新了我的問題。對於那個很抱歉。 – Ali 2012-04-19 22:45:44