Skip to main content

News Website

Many sites contain news listings, such as journalistic sites or company sites with a "News" section. We will call this type of content "Articles". Below is a series of requirements that are generally requested in managing articles:

  • the articles are sorted from most recent to least recent
  • there are important items that we want to keep top of mind in the list
  • articles may have a publication date shown on the site that is different from the actual date of insertion in the CMS
  • articles can also have complex formatting, mixing texts with images and videos
  • on the home page I want to show N articles of the most important ones
  • if I have a hero section, I want to decide which article appears as the hero on my home page

Below is a series of attributes that we typically want to use when defining an Article content type:

  • slug (string that identifies the article in the browser URL)
  • titolo/title (fixed length string, e.g. 1024)
  • subtitle/subtitle (fixed length string, e.g. 1024)
  • body/body (simple or advanced richtext)
  • keywords (taxonomy)
  • publication date visible on the site/displayed publishing date
  • author/author
  • related articles (reference to other articles)
  • show on top/sticky on top (boolean flag)

If we want to give the possibility to modify the page that shows the single article, we will define a template page. This template page will have all the necessary components to display the article and via REST API we will fetch both the content and the page. You can do this with a single REST call by passing the page={slug} parameter when retrieving the content. On the frontend side I will use <DiscoveryComponent/> to build the page dynamically.

Alternatively, we decide that the page displaying the articles is not editable, and in that case it will be coded directly with the frontend framework, without using <DiscoveryComponent/>.

Example Query

It is possible to retrieve Article-type contents according to various criteria, as explained in the getContents() method section. If your application is in React, instead of getContents() you can use the useDiscoveryContents() hook like in this example:

// Retrieve all contents of type "Article", sorted by title (A-Z), maximum 3 results.
const contents = useDiscoveryContents({
        type: 'Article',
        sort: 'title asc',
        start: 0,
        limit: 3
});

which retrieves the following object containing the following results:

{
  "resultCount": 25,
  "entities": [
    {
"title": "I will take care of the financial investment now as a pillow",
      "preview": "/demo/dam/core/img/512x512/no-preview.png",
"slug":
      "_id": "8bef2241-5290-44fc-8b76-771d458f27ab"
    },
    {
"title": "On the Limits of Good and Evil",
      "preview": "/demo/dam/core/img/512x512/no-preview.png",
"slug": "Boundary",
      "_id": "75b58e0e-510a-41ca-90c1-5df5ceb924e4"
    },    
    {
"title"
      "preview": "/demo/dam/core/img/512x512/no-preview.png",
"slug": "lore",
      "_id": "58e17ccd-a41c-4aba-8b20-8f6253c227de"
    }
  ]
}

Note that:

  • resultCount indicates the TOTAL results of the query and not the returned results, which are limited by the limit option. This option, together with start, allows you to retrieve results in a paginated manner.
  • sorting by title is not case sensitive.
  • not all the fields of the "Article" content type are displayed, but only those that are visible in the listing by default (_id, slug, title and preview). To show additional fields you need to choose the "Available in Listing API" option for the fields you want to add, because by default the API tries to keep the number of fields returned in queries small. If this option changes, you must republish the content for the new attributes to be visible in searches.

Let's now apply a couple of filters to the same query by adding the filter option:

// Retrieve all contents of type "Article", sorted by title (A-Z), maximum 10 results.
const contents = useDiscoveryContents({
        type: 'Article',
        sort: 'title asc',
        start: 0,
        limit: 3,
filters: {
author: 'Andrea',
            'displayed_publishing_date[bt]': '2023-01-01T00:00:00.000Z'
}
});

We are filtering for articles that have author = 'Andrea' and a publication date visible to users from 1/1/2023 onwards. The structure of the results list, in this case, will be the same.

Hero News Management

On the home page, insert a Hero component that has a reference to an article. On the editorial side, you can use the CMS picker to choose the Hero article.

Top News Management

A typical pattern, used for example in journalistic sites, is to show the first N news on the home page, and then refer to search or taxonomic navigation to view all other articles. In this case, it is recommended to use an "editorial publication date" attribute (different from the actual publication date) that establishes the order in which the main stories are displayed. Combining, therefore, an ordering on the publication date editorial with a limit on the contents retrieved by the getContents() method, it is possible automatically populate the home page.

If the customer requests further control over which articles should appear on the home page, it is recommended to add a flag (e.g. "featured"), in order to filter only the featured articles for the home page, always maintaining sorting by editorial publication date and limiting the maximum number of items retrieved.