DEV Community

Cover image for Create a Google News sitemap with Ghost
Ryan Feigenbaum for Ghost

Posted on • Originally published at ghost.org on

Create a Google News sitemap with Ghost

Add your content to Google News by adding a Google News sitemap to your Ghost site. The sitemap lets Google News know all the important details about your published content.

This tutorial walks you through how to create a Google News sitemap by implementing a new route on your Ghost site and by creating a custom template that is fully optimized for the Google News aggregator.

Add a new route for your sitemap

To make your sitemap accessible on the web, add a new route using Ghost's dynamic routing layer.

Download the most up-to-date version of your routes.yaml file from Ghost Admin. Go to Settings → Labs → Download current routes.yaml. Open the file in your code editor of choice.

Create a Google News sitemap

Add the following route:

routes:
  /sitemap/:
    template: sitemap
    content_type: text/xml
Enter fullscreen mode Exit fullscreen mode

This entry tells Ghost to create a route at yoursite.com/sitemap/, and on that route, to serve the sitemap template file and send an XML response (which is what Google News expects).

Your routes.yaml file should now look like the file below, plus any other changes you may have made to it.

routes:
  /sitemap/:
    template: sitemap
    content_type: text/xml

collections:
  /:
    permalink: /{slug}/
    template: index

taxonomies:
  tag: /tag/{slug}/
  author: /author/{slug}/
Enter fullscreen mode Exit fullscreen mode

Save the file and upload it to Ghost Admin ( Settings → Labs → Upload routes YAML ).

Create a template for the Google News sitemap

The next step is to create a Handlebars template in your theme. This requires a little bit of coding, but the example in this tutorial provides a fully functional starting point.

Create a new file in the root of your theme called sitemap.hbs.

Add your posts

The code below tells Ghost to fetch your latest 1,000 posts and format the information for Google News. The template follows the specifications for a Google News sitemap like including the title and publication date as well as indicating if the content is for members only.

<?xml version="1.0" encoding="UTF-8"?>
  <urlset
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
  >
    {{#get "posts" limit="1000" include="tags"}}
      {{#foreach posts}}
        <url>
          <loc>{{url absolute="true"}}</loc>
          <news:news>
            <news:publication>
              <news:name>{{@site.title}}</news:name>
              <news:language>{{@site.locale}}</news:language>
            </news:publication>
            {{#has visibility="members"}}
            <news:access>Registration</news:access>
            {{/has}}
            {{#has visibility="paid"}}
            <news:access>Subscription</news:access>
            {{/has}}
            <news:publication_date>{{date published_at format="YYYY-MM-DDTHH:mm:ssZ"}}</news:publication_date>
            <news:title>{{title}}</news:title>
            <news:keywords>{{tags limit="5" autolink="false"}}</news:keywords>
          </news:news>
        </url>
      {{/foreach}}
    {{/get}}
  </urlset>
Enter fullscreen mode Exit fullscreen mode

Copy and paste this code to the sitemap.hbs file or customize it to suit your needs. With customization, be aware that Google is stringent about attributes and formatting, so be sure to include all required fields.

Update your theme

Save the new template, zip up your theme, and upload it to your Ghost site. Check to see if your Google News sitemap is active by visiting yoursite.com/sitemap/. (We added a sitemap to this Tutorials site as an example: https://ghost.org/tutorials/sitemap/.)

Create a Google News sitemap

Submit your Google News sitemap

Google Search Console is essential for monitoring your search performance in Google. See our guide on integrating it with Ghost. It's also the best way to let Google know about your new Google News sitemap.

In Google Search Console, go to Sitemaps and add your new URL to the Add a new sitemap box. Click Submit. The sitemap should be fetched immediately, but occasionally it can take some time for Google to crawl the URLs listed in it. A successful status indicates that Google has indexed all of your links submitted to Google News. That means your content is available to show up on Google News 📰

Summary

In this tutorial, you used Ghost's routing system and theme layer to create a custom Google News sitemap 🙌

Creating the sitemap helps your content show up in Google News, but Google also provides several other options for customization, monetization, and improved placement. See Google's guide to learn more.

Having trouble with this tutorial or seeing great success with your new Google News sitemap? Come share your experience in the official Ghost Forum, where developers, publishers, and content creators come together to discuss all things Ghost.

Top comments (0)