而不是使用locationimage
属性,为什么不使用附件ID?
这将允许您动态获取文件的URL,并使获取其他元数据(如alt文本)更加容易。以下是的重写版本hplocationsfn()
.
用法示例:[hpLocationSquare id=\'2299\' link=\'http://example.com\']
请注意link
属性是可选的。
function hplocationsfn( $atts ) {
// Set up shortcode attributes.
// User provided values are stored in $atts.
// Default values are passed to shortcode_atts below.
// Merged values are stored in the $a array.
$a = shortcode_atts( [
\'id\' => false,
\'size\' => \'thumbnail\',
\'link\' => false,
], $atts );
// Bail if we don\'t have an image.
if ( ! $a[\'id\'] ) {
return;
}
// Get the image and the image\'s alt value.
$image = wp_get_attachment_image_src( $a[\'id\'], $a[\'size\'] );
$image_alt = get_post_meta( $a[\'id\'], \'_wp_attachment_image_alt\', true );
// Bail if our image is invalid.
if ( ! $image || empty ( $image ) ) {
return;
}
// Generate the output.
$output = \'<div class="locationSquare">\';
if ( $a[\'link\'] ) {
$output .= \'<a href="\' . esc_attr( $a[\'link\'] ) .\'">\';
}
$output .= \'<img src="\' . esc_attr( $image[0] ) . \'" alt="\' . esc_attr( $image_alt ) . \'"/>\';
if ( $a[\'link\'] ) {
$output .= \'</a>\';
}
$output .= \'</div><!-- locationSquare -->\';
return $output;
}
add_shortcode( \'hpLocationSquare\', \'hplocationsfn\' );
对于用户来说,查找附件ID可能有点困难,因此,以下是一个将附件ID添加到媒体覆盖屏幕的功能:
/**
* Add custom field to media
*/
add_filter( \'attachment_fields_to_edit\', \'wpse_attachment_fields_id\', 10, 2 );
function wpse_attachment_fields_id( $fields, $post ) {
$fields[\'attachment_id\'] = [
\'label\' => __( \'Attachment ID\', \'text-domain\' ),
\'input\' => \'html\',
\'value\' => $post->ID,
\'html\' => "<input type=\'text\' class=\'text attachment_idfield\' readonly=\'readonly\' name=\'attachments[{$post->ID}][attachment_id]\' value=\'" . esc_attr( $post->ID ) . "\' /><br />",
\'show_in_edit\' => true,
];
return $fields;
}
编辑:从图像URL获取附件ID或者,我们可以使用
attachment_url_to_postid()
.
Example usage:
[hpLocationSquare2 locationimage=\'http://domain.com/wp-content/uploads/2017/02/my-image.jpg\' link=\'http://example.com/2/\']
function hplocationsfn2( $atts ) {
// Set up shortcode attrubutes.
// User provided values are stored in $atts.
// Default valued are passed to shortcode_atts below.
// Merged valued are stored in the $a array.
$a = shortcode_atts( [
\'locationimage\' => false,
\'link\' => false,
], $atts );
// Bail if we don\'t have an image.
if ( ! $a[\'locationimage\'] ) {
return;
}
$image_id = attachment_url_to_postid( $a[\'locationimage\'] );
if ( ! $image_id ) {
return;
}
// Get the image and the image\'s alt value.
$image_alt = get_post_meta( $image_id, \'_wp_attachment_image_alt\', true );
// Generate the output.
$output = \'<div class="locationSquare">\';
if ( $a[\'link\'] ) {
$output .= \'<a href="\' . esc_attr( $a[\'link\'] ) .\'">\';
}
$output .= \'<img src="\' . esc_attr( $a[\'locationimage\'] ) . \'" alt="\' . esc_attr( $image_alt ) . \'"/>\';
if ( $a[\'link\'] ) {
$output .= \'</a>\';
}
$output .= \'</div><!-- locationSquare -->\';
return $output;
}
add_shortcode( \'hpLocationSquare2\', \'hplocationsfn2\' );