• Fields

Content Relationship

This article explains what the content relationship field is and how to configure it.

Content writers can link documents through content relationship fields. Developers can then access content from those documents.

Content relationship fields are often used for:

  • Connecting a blog post to its author.
  • Displaying a testimonial on a landing page.
  • Linking a product to its category.

Add a content relationship to a content model

  • Open Slice Machine

    In your Prismic project, start Slice Machine to begin editing content models.

    npx start-slicemachine --open
  • Add a content relationship field

    In Slice Machine, navigate to the slice, page type, or custom type you want to modify. Add a content relationship field.

    The label determines the label shown to content writers in the Page Builder. Use an easily understood name.

    The API ID determines the property name in the Document API. Use a short, snake-cased name.

  • Select the allowed document type

    You can restrict the field to a specific document type. For example, you can configure the field to only allow Author documents in an Author field.

    In the field’s settings, click the Add type button to select a document type.

    You’ll be able to select fields from that document type to include in the API response in the next step.

  • Select the fields to fetch

    If you restrict the field to a specific document type, you can select which fields to include in the API response.

    Under the Allowed type section, use the field picker to choose which fields should be included. Up to 2 levels of nested fields can be selected (e.g. blog -> author -> profession).

    The selected fields are included in Slice Machine’s generated TypeScript types.

Display content relationships

Content from a related document can be displayed like any other field. The name of a blog post’s author, for example, can be rendered using the <PrismicText> component.

Before accessing nested content, use isFilled.contentRelationship() to ensure the relationship has a document.

{
  isFilled.contentRelationship(post.data.author) && (
    <p>
      Written by <PrismicText field={post.data.author.data?.name} />,
      {isFilled.contentRelationship(post.data.author.data?.profession) && (
        <PrismicText field={post.data.author.data.profession?.data.name} />
      )}
    </p>
  );
}

Examples

Blog post authors

Landing page testimonials

Taxonomies

Nested menus

API response

Here is what a content relationship looks like from the Document API:

{
  "author": {
    "id": "XxnD3REAACYAk_CJ",
    "type": "author",
    "tags": [],
    "slug": "ada-lovelace",
    "lang": "en",
    "uid": "ada-lovelace",
    "data": {
      "name": [
        {
          "type": "paragraph",
          "text": "Ada Lovelace",
          "spans": []
        }
      ],
      "profession": {
        "id": "ZpqX7SFJJKEBl_VK",
        "type": "profession",
        "tags": [],
        "slug": "mathematician",
        "lang": "en",
        "uid": "mathematician",
        "data": {
          "name": [
            {
              "type": "paragraph",
              "text": "Mathematician",
              "spans": []
            }
          ]
        },
        "link_type": "Document",
        "isBroken": false
      }
    },
    "link_type": "Document",
    "isBroken": false
  }
}

The data property’s contents is determined by the selected fields.