我試圖用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() ?>
你試過var_dump($ image)? – Hajo 2012-04-19 22:22:05
@Hajo你能給我更多的提示或信息嗎?我不擅長PHP,因爲我將在今年夏天參加PHP課程。 – Ali 2012-04-19 22:22:54
<?php print_r($ image); ?>插入問題附近的某個地方進行測試,然後查看[0]鍵是否包含您的url丟失的數據,或者是否有另一個或甚至不在那裏。確保從源頭切斷之後它;) – Hajo 2012-04-19 22:25:02