Developer docs

How to create a Google News Sitemap with Ghost

A sitemap contains detailed information about your posts and allows search engines to crawl for new content quickly and easily. A Google News sitemap lets you control which content you submit to Google News specifically.

This tutorial walks you through how to create a Google News sitemap using dynamic routing, as well as a Handlebars template for your posts that is fully optimised for the Google News aggregator.

Add a new route for your sitemap

The first thing to do is add a new route where your sitemap will exist by the using the dynamic routing layer in Ghost. Download the most up to date version of your routes.yaml file from Ghost Admin settings menu and open it in your code editor of choice.

For the purposes of this example, we’ll add this to the root of our site – here’s what it looks like:

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

We’re telling the routes file that we expect an XML response when visiting our sitemap endpoint.

Create a new template for Google News sitemap

Now that you’ve added the route you want to use for the sitemap to your routes.yaml, you’ll need to create a new Handlebars template in your theme. This requires a little bit of coding, but you can use the example provided in this tutorial as a starting point.


Add your posts for Google News to aggregate

In order for the Google News aggregator to reliably generate content based on your site we need to make sure to format the Handlebars template with the appropriate fields and structure. Below is an example of how to structure your XML with all of your recent posts so Google can quickly crawl your site. You can copy this into your own template and replace with necessary information:

<?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" order="published_at desc" include="tags"}}
    {{#foreach posts visibility="all"}}
    <url>
        <loc>{{url absolute="true"}}</loc>
        <news:news>
        <news:publication>
            <news:name>{{@site.title}}</news:name>
            <news:language>{{lang}}</news:language>
        </news:publication>
        {{#if @labs.members}}
            {{#has visibility="member"}}
            <news:access>Registration</news:access>
            {{/has}}
            {{#has visibility="paid"}}
            <news:access>Subscription</news:access>
            {{/has}}
        {{/if}}
        <news:genres>Blog</news:genres>
        <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>

You can copy and paste this exact implementation for your site, or customise it to suit your needs! Google is very strict with which attributes are required and the format they are presented in. You can can read more about the Google News sitemap specification on their website.

If you’re using Members you’ll want to specify the access level news:access of the given post so Google knows if this article is public or requires membership.

Update routes.yaml and your active theme

Once you’re happy with your work, upload a new version of routes.yaml and update your active theme in Ghost Admin to enable your new Google News sitemap. Once you have done this, you should be able to visit the feed at /sitemap/ to ensure it’s working as desired and submit it to Google.

Validate your Google News sitemap

To validate your sitemap has been created successfully use an XML Validator like the one found on Truggo. By clicking ‘Load URL’ and entering the route we defined above the validator will fetch your sitemap and tell you if it’s been formatted correctly.


Summary

Success! You should have implemented your very own custom Google News sitemap for crawling your new posts on your Ghost site using dynamic routing and a Handlebars template. Don’t forget you can get more in-depth information about the Handlebars theme layer in Ghost in the docs.