这就是我最终编写解决方案的原因:
$images = get_post_custom_values( \'images\' );
if ( $images ) {
echo \'<div class="image-wrap">\';
if ( $image_header ) {
echo \'<div class="header">\' . esc_html( $image_header ) . \'</div>\';
}
foreach ( (array) $images as $image ) {
$url = esc_url( $image );
$alt = esc_html( the_title_attribute( \'echo=0\' ) );
if ( $url ) {
echo \'<img class="image-section" src="\' . $url . \'" alt="\' . $alt . \'" />\';
}
}
echo \'</div>\';
}
To get the i.d from a URL
您可以使用
attachment_url_to_postid 像这样:
$url = \'http://example.com/img.png\';
$id = attachment_url_to_postid( $url );
$alt = get_post_meta( $id, \'_wp_attachment_image_alt\', true );