Get latest posts inside author looping

I need to create a author list which will hold authors info as well as his/her latest 4 posts. My design will be look like this,

image

Here is my approch after searching google and digging in docs.

{{#get 'users' limit="all" include="count.posts,posts"}}
        {{#foreach users}}
            {{!-- user datas --}}
            {{name}} , {{bio}} // ... and so on

            {{!-- This user's posts --}}
            {{#foreach posts limit="3"}} 
                <a href="{{ url }}"> {{ title }} </a>
            {{/foreach}}

        {{/foreach}}
{{/get}}

I’m getting user data perfectly but can’t get individual user’s posts by this,

{{#foreach posts limit="3"}} 
     <a href="{{ url }}"> {{ title }} </a>
{{/foreach}}

Please make me correct.
Thanks.

As per the documentation, when you get the users resource you can only include “count.posts” . Ghost Handlebars Themes - Building a custom Ghost theme - Docs

The User and Tag resources can be expanded to include the post count for each resource.

Include options for User and Tag : “count.posts”

You can try to nest another get helper request. Although as per the documentation ( Ghost Handlebars Themes - Building a custom Ghost theme - Docs )

  • {{#get}} helpers may not work correctly when nested

You can try this

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

I have not tested this, not sure will it work or not, but you can try this approach.

1 Like

Thanks for your helping approach, unfortunately this nested get helper is not working in my theme :’(

Here is the right way to do this:

{{#get "users" limit="all" include="count.posts"}}
	{{#foreach users}}
		{{#if count.posts}}
			<h3>{{name}}</h3>
			<ul>
			{{#get "posts" filter="primary_author:{{slug}}" limit="3"}}
				{{#foreach posts}}
			  		<li><a href="{{url}}">{{title}}</a></li>
				{{/foreach}}  
			{{/get}}
			</ul>		
		{{/if}}
  	{{/foreach}}
{{/get}}

First we fetch all the users. Then we parse them. We check if the user has any posts. If it doesn’t we don’t show it. Then we fetch 3 posts where the user is the primary author. That is it.

3 Likes

Thanks @HauntedThemes , it works :slight_smile: