如何在Wordpress管理面板中为自定义帖子类型添加媒体附件

我需要以这样一种方式来实现管理面板中的功能:我可以为特定自定义帖子类型的图片库添加图像,也可以查看/编辑从管理面板上传的所有图像。

P.S我想在主题中开发此功能而不使用任何插件。

niksosf 回答:如何在Wordpress管理面板中为自定义帖子类型添加媒体附件

请看以下内容:

  • 首先,如果要向帖子中添加多个图片,请安装ACF插件并创建一个Repeater字段。

如果要在ACF转发器字段的所有页面上将所有帖子图像显示为画廊,那么Plesae的外观如下:

<?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; ?>

如果要显示不带中继器字段的图像,请在下面查看:

<?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].'">';



     }

}

这将在下面的自定义字段中显示附加到帖子的所有图像。

本文链接:https://www.f2er.com/2926965.html

大家都在问