Watch a live product demo 👀 See how Sanity powers richer commerce experiences

Sanity Media Library - How to Add Images to a Slide Show

1 replies
Last updated: Nov 25, 2021
I'm building a prototype CMS in Sanity for evaluation with our highly-structured metadata and content. Having dealt with very poor media managers in Wordpress and Drupal, I was a little disappointed not to find better media management in Sanity. When dealing with 1000's of images, it's important that a) metadata travels with images (don't need to re-type alt & title tags), b) it's easy to search for images by metadata, c) you can easily update an image with a new version. I tried out https://github.com/aratramba/sanity-plugin-media-library which seems nice and allows me to add custom fields. But ideally I don't want to rely on a third-party plugin if possible. Then I realized I could just make a new content type, "photo", that specifies all the metadata I need along with an image of type "image" (Sanity image type).
export default {
  type: 'document', 
  name: 'photo', 
  title: 'Photo', 
  fields: [
    { type: 'string', name: 'title', title: 'Title' }, 
    { type: 'string', name: 'alt', title: 'Alt' },
    { type: 'string', name: 'artist', title: 'Artist'}, 
    { type: 'string', name: 'movement', title: 'Movement' },
    { type: 'string', name: 'period', title: 'Period' },
    { name: "image", type: "image", title: "Image" }
  ]
}
Took me all of 10 minutes to create the type and add it as a reference to a "carousel" slide show.

  title: 'Slides',
  name: 'slides',
  type: 'array',
  of: [
    {
      type: 'reference',
      to: [{type: 'photo'}],
      name: 'slide',
      title: 'Slide'
    }
  ]
I can now add "photos" to any other content type, update the photo metadata or even upload a new image for that photo, and search for images by the content type fields. Super powerful! Have any of you done something similar? Or do you find that a media library plugin works better? Am I missing some feature for which I should use a plugin instead? (btw I'll probably rename it from "photo" to "artwork".)
Nov 23, 2021, 10:03 PM
Hi! Yes, I also chose this approach for several projects. I also remember asking about it and somebody from Sanity told me it's a good approach. Plus you get a warning when trying to delete an image that is already used somewhere.
Nov 25, 2021, 4:20 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing