Skip to content

Working With Ghost Authors / Practical Examples to Use in Your Theme

I discussed how the Ghost Author Context works in the latest post. This practical post will show how to work with Ghost Author API. I will show examples of displaying all the website authors, show-specific authors, exclude some and more cases to use in your Ghost theme.

I will use the Ghost #get block helper to make custom queries to the Ghost API to get the author’s data.

Show All Authors

Let’s start with a basic example to show all the website authors.

{{#get 'authors' limit='all'}}
  {{#foreach authors}}
    {{ name }}
  {{/foreach}}
{{/get}}

We have used the limit attribute with the all value to specify how many authors we want in return. In this case, we passed the all value to get all authors.

The default value of the limit attribute is 15. In addition to the all value, we can pass a number; for example, if we want to get five authors, we can use limit='5'.

{{#get 'authors' limit='5'}}
  {{#foreach authors}}
    {{ name }}
  {{/foreach}}
{{/get}}

In addition to the #get block helper, I used the foreach, loop helper, to iterate over the data one by one and then output the author content inside its opening and closing tags {{#foreach}}{{/foreach}}.

The final result of the previous example will be.

  Ahmad Ajmi
  William James
  Emily James
  Jeff Rosenberger
  Olivia Thomas

As you can see, the only date we get so far is the author name using the {{#foreach}}{{ name }} attribute. Let’s see how we can make the author name clickable to go to the author page when clicking on it.

The following example will output all the author as an HTML unordered list.

{{#get 'authors' limit='5'}}
  {{#foreach authors}}
    <a href='{{ url }}'>{{ name }}</a>
  {{/foreach}}
{{/get}}

Here, I used the {{#foreach}}{{ url }} attribute, which will output the author URL within an HTML link tag.

The result will be.

  <a href="/author/ahmad/">Ahmad Ajmi</a>
  <a href="/author/william/">William James</a>
  <a href="/author/emily/">Emily James</a>
  <a href="/author/jeff/">Jeff Rosenberger</a>
  <a href="/author/olivia/">Olivia Thomas</a>

For the list of the available attributes, check Author object attributes.

To create a custom authors page, read my Create an Authors Page post.

Show Specific Authors

To show only a specific author, use the filter attribute and pass the Slug of each tag you want to get.

You can find the tag Slug on the tag setting page.

Ghost Author Slug

In this case, the basic syntax for the #get block helper is.

{{#get 'authors' filter='slug:[SLUG, SLUG, SLUG]'}}

Note the Comma (,) separating each slug.

If we want to get the authors with the ahmad, eiad and adham slugs, the previous example will be like.

{{#get 'authors' filter='slug:[ahmad, eiad, adham]'}}
  {{#foreach authors}}
    <a href='{{ url }}'>{{ name }}</a>
  {{/foreach}}
{{/get}}

Note

This way does not guarantee getting the same authors in the same order as you added the slugs. Probably a Ghost issue. Instead, use the following method.

In this method, we get each author separately. For example, to get the author with ahmad and eiad slugs.

{{#get 'authors' filter='slug:ahmad'}}
  {{#foreach authors}}
    <a href='{{ url }}'>{{ name }}</a>
  {{/foreach}}
{{/get}}

{{#get 'authors' filter='slug:eiad'}}
  {{#foreach authors}}
    <a href='{{ url }}'>{{ name }}</a>
  {{/foreach}}
{{/get}}

I removed the [ ] around the slug as we are only filtering with one slug.

Exclude Authors

Suppose you want to show all authors except a few ones. For example, you want to show all the website authors but the authors with ahmad and eiad slugs.

In this case, the basic syntax for the #authors helper is.

{{#get 'authors' filter='slug:-[SLUG, SLUG, SLUG]'}}

The difference from the previous example is the - sign in the filter attribute before the slugs. Note the following example.

{{#get 'authors' filter='slug:-[ahmad, eiad]'}}
  {{#foreach authors}}
    <a href='{{ url }}'>{{ name }}</a>
  {{/foreach}}
{{/get}}

Show Post Count

Another practical example is to show the post count for each author. Use the include attribute and pass the count.posts as a value. Let’s see, we want to show five authors and how many posts each one has.

{{#get 'authors' limit='5' include='count.posts'}}
  {{#foreach authors}}
    <a href='{{ url }}'>{{ name }} ({{ count.posts }})</a>
  {{/foreach}}
{{/get}}

The result will be.

  <a href="/author/ahmad/">Ahmad Ajmi (4)</a>
  <a href="/author/william/">William James (3)</a>
  <a href="/author/emily/">Emily James (2)</a>
  <a href="/author/jeff/">Jeff Rosenberger (6)</a>
  <a href="/author/olivia/">Olivia Thomas (8)</a>

We can also order the authors by their post count. In the following example, I used the order attribute to order by the count.posts we used before in the include attribute.

{{#get 'authors' limit='5' include='count.posts' order='count.posts desc'}}
  {{#foreach authors}}
    <a href='{{ url }}'>{{ name }} ({{ count.posts }})</a>
  {{/foreach}}
{{/get}}

The result will be.

  <a href="/author/olivia/">Olivia Thomas (8)</a>
  <a href="/author/jeff/">Jeff Rosenberger (6)</a>
  <a href="/author/ahmad/">Ahmad Ajmi (4)</a>
  <a href="/author/william/">William James (3)</a>
  <a href="/author/emily/">Emily James (2)</a>

Show Posts for Each Author

Let’s take a more complex example. Suppose we want to list five authors ordered by how many posts they have and show their posts under each.

{{#get 'authors' limit='5' include='count.posts' order='count.posts desc'}}
  {{#foreach authors}}
    <h4><a href='{{ url }}'>{{ name }}</a></h4>

    {{#get 'posts' filter='primary_author:{{slug}}' limit='3'}}
      <ul>
        {{#foreach posts}}
          <li><a href='{{ url }}'>{{ title }}</a></li>
        {{/foreach}}
      </ul>
    {{/get}}
  {{/foreach}}
{{/get}}

I added a new #get block helper starting from line five inside the foreach helper to get the posts associated with each author filtered by his slug. I also set the limit to three posts, but you are free to customize this.

For example, if the author slug is ahmad, I’m asking Ghost to get the posts associated with him using the filter attribute and pass the {{slug}} as a value to primary_author. The {{slug}} is already available for each author as a data value like name and image, and in this case, it will be rendered as ahmad.

So, the code for the posts helper will be interpreted as follows.

{{#get 'posts' filter='primary_author:ahmad' limit='3'}}
  {{#foreach posts}}
    <li><a href='{{ url }}'>{{ title }}</a></li>
  {{/foreach}}
{{/get}}

This will happen for each author within the loop. The result will look like the following example. Each author and their posts are underneath.

<h4><a href="/author/eiad/">Jeff Rosenberger</a></h4>
<ul>
  <li><a href="/a-rich-life/">A Rich Life with Less Stuff</a></li>
  <li><a href="/it-would-be/">The Rappers Who Are Breaking Up</a></li>
  <li><a href="/themes/">Expertise Is Falling Out of Favor</a></li>
</ul>

<h4><a href="/author/ahmad/">Ahmad Ajmi</a></h4>
<ul>
  <li><a href="/travel/">Travel Is No Cure for the Rediverge</a></li>
  <li><a href="/my-ultimate-reading/">My Ultimate Reading List</a></li>
  <li><a href="/untitled/">10 Trends for Creators to Watch in 2021</a></li>
</ul>

<h4><a href="/author/adham/">Olivia Thomas</a></h4>
<ul>
  <li><a href="/organising/">How to Get More Out of Your Performance</a></li>
  <li><a href="/ghost-publishing/">Will John Roberts Constrain Trump?</a></li>
  <li><a href="/days-of-wine/">The Film That You Might Not Hear About</a></li>
</ul>

<h4><a href="/author/sara/">Sara James</a></h4>
<ul>
  <li><a href="/managing-ghost/">How to Think About the Plummeting Stock</a></li>
  <li><a href="/the-new-rules/">The New Rules of Personal Finance</a></li>
  <li><a href="/eddie-kim-somehow-/">Eddie Kim somehow managed</a></li>
</ul>

That’s all that I wanted to share today, and I hope you find this post helpful.

tutorials

Latest Ghost Themes

Use code TODAY for 20% off Don’t miss out on this offer!