使用找到的函数here, 您可以将其添加到函数中。php
// retrieves the attachment ID from the file URL
function pippin_get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid=\'%s\';", $image_url ));
return $attachment[0];
}
但要使用它,您需要完整的图像URL。
$img_url = get_bloginfo(\'url\') . \'/bc/wp-content/uploads/placeholder-image.png\';
if ( $img_id = pippin_get_image_id($img_url) ) {
// Using wp_get_attachment_image should return your alt text added in the WordPress admin.
echo wp_get_attachment_image( $img_id, \'full\' );
} else {
// Fallback in case it\'s not found.
echo \'<img src="\' . $img_url . \'" alt="" />\';
}