2013-04-11 55 views
0

發佈帖子時,我想附上圖片標識。我使用下面的代碼: -發佈帖子時附上圖片標識

function my_function($post_id){ 
    echo $imgId = get_post_thumbnail_id($post_id); die; 
    return $post_id; 
} 
add_filter('publish_post', 'my_function'); 

它不給圖片ID。 當我編輯並保存現有的帖子時,它會返回圖片ID。

請幫忙.......

+0

你想獲得功能圖像ID? – anstrangel0ver 2013-04-11 05:57:05

+0

@ anstrangel0ver yea ... – user2131144 2013-04-11 06:14:58

+0

嘗試使用'global $ post'和'$ post-> id' – SMacFadyen 2013-04-11 07:19:09

回答

0

這就是我如何得到精選圖片。

if (has_post_thumbnail()){ 
    $image_id = get_post_thumbnail_id(); 
    $alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true); 
    $image_url = wp_get_attachment_image_src($image_id,'large'); 
    $image_url = $image_url[0]; 
}