我使用了一个插件(Meta box 4.8.3)来创建自定义元框并在自定义帖子中获取值。如果自定义元框字段是文本,则该函数返回该值。如果是图像则不显示图像。
**注册自定义元框**
add_filter( 'rwmb_meta_boxes', 'your_prefix_meta_boxes' );
function your_prefix_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array(
'title' => __( 'Image Gallery', 'textdomain' ),
'post_types' => 'events',
'fields' => array(
array(
'name' => __( 'Image Upload', 'your-prefix' ),
'id' => $prefix . 'test_image',
'type' => 'image_advanced',
),
),
);
return $meta_boxes;
}
要获取图像代码是
function getgallery(){
//global $post;
$image = wp_get_attachment_image( get_post_meta( get_the_ID(), 'test_image',1));
if ( !empty( $image ) ) {
foreach ( $image as $images ) {
?>
<div class="eventsinfo-detail">
<div class="row events-info">
<div class="col-sm-3">
<div class="events-detail">
<div class="event-image">
<a href="#"><?php echo $image; ?></a>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
}
如何显示通过仪表板上传的图像。
问题已更新
答案 0 :(得分:1)
请尝试以下代码并确认代码是否有效?:
显示单张图片
$attachment_id = get_post_meta($post->ID, 'test_image', true);
echo wp_get_attachment_image(attachment_id, 'thumbnail');
显示多张图片
$attachment_ids = get_post_meta($post->ID, 'test_image');
foreach($attachment_ids AS $attachment_id){
echo wp_get_attachment_image($attachment_id, 'thumbnail');
}
您的代码试试这个
function getgallery(){
$images = wp_get_attachment_image( get_post_meta( get_the_ID(), 'test_image'));
if ( count( $images ) > 0 ) {
foreach ( $images AS $image ) {
?>
<div class="eventsinfo-detail">
<div class="row events-info">
<div class="col-sm-3">
<div class="events-detail">
<div class="event-image">
<a href="#"><?php echo $image; ?></a>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
答案 1 :(得分:1)
$attachment_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_parent' => get_the_ID(),
);
$images = new WP_Query($attachment_args);
if ($images->have_posts()) :
while ($images->have_posts()) : $images->the_post();
$img = wp_get_attachment_image_src(get_the_ID(),'test_image'); // where get_the_ID() is id of attachment
echo '<img src="'. esc_url( $img[0]) .'" />';
endwhile;
endif;
wp_reset_postdata();
立即尝试更新的代码......