我正在使用Custom MetaBoxes 2创建图片上传器字段。
以下是元数据的代码:
$meta_boxes['home_page_slider'] = array(
'id' => 'home_page_slider',
'title' => __( 'Home Page Slider', 'cmb2' ),
'object_types' => array( 'page', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'show_on' => array( 'id' => array( 5, ) ), // Specific post IDs to display this metabox
'fields' => array(
array(
'name' => __( 'Slider Images', 'cmb2' ),
'desc' => __( 'Upload or add multiple images/attachments.', 'cmb2' ),
'id' => $prefix . 'slider-images',
'type' => 'file_list',
'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
),
)
);
如何检索上传到此字段但具有特定大小的图像(例如“完整”),并在前端的下方HTML标记上显示这些图像?
<ul class="slides">
<li>
<img src="images/awn1.jpg" />
</li>
<li>
<img src="images/awn2.jpg" />
</li>
<li>
<img src="images/awn3.jpg" />
</li>
</ul>
答案 0 :(得分:4)
这段代码可以解决问题:
<ul class="slides">
<?php
$images = get_post_meta( 5, 'slider-images', true);
if ( $images ) {
foreach ( $images as $attachment_id => $img_full_url ) {
$full = wp_get_attachment_link($attachment_id, 'full');
echo "<li>";
echo $full;
echo "</li>";
}
}
?>
</ul>
享受它,如果你觉得它有用,请评价我的答案。
谢谢。 最好, EF
答案 1 :(得分:0)
检查https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Field-Types#file处的文档,有一个示例显示您想要的内容。
array(
'name' => 'Test File',
'desc' => 'Upload an image or enter an URL.',
'id' => $prefix . 'test_image',
'type' => 'file',
'allow' => array( 'url', 'attachment' ) // limit to just attachments with array( 'attachment' )
),
$image = wp_get_attachment_image( get_post_meta( get_the_ID(), 'test_image_id', 1 ), 'medium' );
其中get_the_ID
是您将图片上传到