Skip to content

Ghost Authors: Practical Theme Examples

By Ahmad Ajmi

I explain Ghost Author Context in this post. Here, I will show how to use the Ghost Author API in a theme. You can list authors, select specific authors, or exclude some. Other examples show their posts and post counts.

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. This example uses all, but current Ghost limits each query to 100 results. For more authors, use the page attribute.

The limit attribute defaults to 15. You can use a number instead of all. For five authors, use limit='5'.

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

I also used the foreach loop helper. #get fetches the data. foreach loops through it one author at a time. It outputs the content between its opening and closing tags: {{#foreach}}{{/foreach}}.

The final result of the previous example will be.

  William James
  Emily James
  Jeff Rosenberger
  Olivia Thomas

As you can see, the only data 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 outputs links for up to five authors.

{{#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/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 author you want to get.

You can find an author’s slug in their staff profile in Ghost Admin.

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. A slug filter selects the authors; it does not set their display order. To choose the sequence, 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 #get 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/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. The next example sorts by count.posts with the order attribute. The include attribute supplies that post count.

{{#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/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}}

The new #get block starts on line five, inside the foreach loop. It filters posts by each primary author’s slug. I set the limit to three posts. You can change that limit.

The filter uses primary_author with the value {{slug}}. For an author whose slug is ahmad, this fetches posts where he is the primary author. Like the name and image, {{slug}} is part of each author’s data. Here, its value is 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/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.

More from the Blog

Latest Ghost Themes