Get latest posts inside author looping


#1

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,

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.


#2

As per the documentation, when you get the users resource you can only include “count.posts” . https://themes.ghost.org/docs/get#section--include-

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 ( https://themes.ghost.org/docs/get#section-limitations )

  • {{#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.


#3

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


#4

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.


#5

Thanks @HauntedThemes , it works :slight_smile: