如何在 wordpress 管理面板中为自定义 post 类型添加媒体附件
How to add media attachments for the custom post type in wordpress admin panel
我需要在管理面板中以这样一种方式实现功能,即我可以为特定自定义 post 类型的图库添加图像,而且我还能够 view/edit 所有图像从管理面板上传。
P.S我想在不使用任何插件的情况下在主题中开发这个功能。
请看下面:
- 首先,如果要向 post 添加多于 1 个图像,请安装 ACF 插件并创建转发器字段,否则不需要转发器字段。
如果您想将所有 post 图像显示为来自 ACF 转发器字段的前端画廊,请查看以下内容:
<?php if( have_rows('repeater_field_name') ): ?>
<ul class="slides">
<?php
while( have_rows('repeater_field_name') ): the_row();
$image = get_sub_field('image');
?>
<li class="slide">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
如果想显示没有repeater字段的图片,请看下面:
<?php
$image = get_field('instructor-image');
if( !empty($image) ):
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
您可以实现无需任何插件即可调用附件的自定义字段。请参考以下代码:
add_action('admin_init', 'show_custom_post_images');
function show_custom_post_images() {
add_meta_box(
'Pictures ',
__('Images attached to post ', 'domain'),
'post_image',
'post_type',
'normal',
'default'
);
}
function post_image() {
global $post;
$arguments = array(
'numberposts' => - 1,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'exclude' => get_post_thumbnail_id() ,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$post_attachments = get_posts($arguments);
foreach ($post_attachments as $attachment) {
$preview = wp_get_attachment_image_src(
$attachment->ID, 'wpestate_slider_thumb'
);
echo '<img src="' . $preview[0] . '">';
}
}
这将在下面的自定义字段中显示附加到 post 的所有图像。
我需要在管理面板中以这样一种方式实现功能,即我可以为特定自定义 post 类型的图库添加图像,而且我还能够 view/edit 所有图像从管理面板上传。
P.S我想在不使用任何插件的情况下在主题中开发这个功能。
请看下面:
- 首先,如果要向 post 添加多于 1 个图像,请安装 ACF 插件并创建转发器字段,否则不需要转发器字段。
如果您想将所有 post 图像显示为来自 ACF 转发器字段的前端画廊,请查看以下内容:
<?php if( have_rows('repeater_field_name') ): ?>
<ul class="slides">
<?php
while( have_rows('repeater_field_name') ): the_row();
$image = get_sub_field('image');
?>
<li class="slide">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
如果想显示没有repeater字段的图片,请看下面:
<?php
$image = get_field('instructor-image');
if( !empty($image) ):
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
您可以实现无需任何插件即可调用附件的自定义字段。请参考以下代码:
add_action('admin_init', 'show_custom_post_images');
function show_custom_post_images() {
add_meta_box(
'Pictures ',
__('Images attached to post ', 'domain'),
'post_image',
'post_type',
'normal',
'default'
);
}
function post_image() {
global $post;
$arguments = array(
'numberposts' => - 1,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'exclude' => get_post_thumbnail_id() ,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$post_attachments = get_posts($arguments);
foreach ($post_attachments as $attachment) {
$preview = wp_get_attachment_image_src(
$attachment->ID, 'wpestate_slider_thumb'
);
echo '<img src="' . $preview[0] . '">';
}
}
这将在下面的自定义字段中显示附加到 post 的所有图像。