【发布时间】:2015-03-01 12:16:50
【问题描述】:
我想让 Wordpress 在上传媒体时计算纵横比和方向,而不是在模板中执行此操作。然后我可以调用 images 并询问 image.orientation,它会给我 'portrait' 或 'landscape' 作为值。
例如,上传图片后的样子:
这是可能的还是我必须在前端这样做?
【问题讨论】:
标签: wordpress upload orientation attachment aspect-ratio
我想让 Wordpress 在上传媒体时计算纵横比和方向,而不是在模板中执行此操作。然后我可以调用 images 并询问 image.orientation,它会给我 'portrait' 或 'landscape' 作为值。
例如,上传图片后的样子:
这是可能的还是我必须在前端这样做?
【问题讨论】:
标签: wordpress upload orientation attachment aspect-ratio
你可以为它写一些代码。使用http://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata 获取宽度和高度,然后如果 $width/$height > 1 => 横向,否则为纵向。使用:http://codex.wordpress.org/Function_Reference/wp_update_attachment_metadata 将此值存储在附件中。
您可以为此使用操作,例如:
add_action('add_attachment', 'process_images');
根据您的代码尝试添加解决方案(我已经测试过了):
function attachment_orientation_meta($id){
$attachmentInfo = wp_get_attachment_metadata($id);
$attachmentOrientation = ( $attachmentInfo['height'] > $attachmentInfo['width'] ) ? 'portrait' : 'landscape';
update_post_meta($id, 'orientation', $attachmentOrientation);
}
add_action( 'add_attachment', 'attachment_orientation_meta' );
【讨论】:
// add orientation to attachments function attachment_orientation_meta(){ $attachmentInfo = wp_get_attachment_metadata( $id ); $attachmentOrientation = ( $attachmentInfo['height'] > $attachmentInfo['width'] ) ? 'portrait' : 'landscape'; $attachmentOrientationMeta = wp_update_attachment_metadata( $post_id, $attachmentOrientation ); return $attachmentOrientationMeta; } add_action( 'add_attachment', 'attachment_orientation_meta' );