Forum Discussion

bhupen's avatar
bhupen
Advisor
10 years ago

How to show only 5 posts?

Hi guys,

 

I need to show only 5 posts. Somehow I am able to show the latest posts but My query is not working to show up only 5 posts:

Following is the code I have done:

 

<#list restadmin("/roles/name/Administrator/users").users.user as user>  <#-- REST call to get the user's roles -->
<#list restadmin("/users/id/${user.id}/posts/latest?page_size=5").messages.message as recent>
<a href="${recent.@view_href}">${recent.subject}</a></br>
</#list>
</#list>

 

Out put for the above code:

Its showing more then 8 posts at front end

Can any one tell me how can I show only 5 posts?

  • bhupen 

     

    You can mix API v1 and API v2 calls.

    If you want to optimize the code, have a look at the User resource in the API v2 documentation and check if you can get the favourite.

     

    Here is the code that should work. You need to provide a valide user.id for your rest call.

    It will loop the user favourite and build a list for the WHERE CLAUSE used to get the last 5 messages.

     

    <h3>Latest Posts from Favorites Users</h3> <br />
    <#assign whereclause = ""/>
    <#list restadmin("/users/id/${user.id}/addressbook/contacts/friends").users.user as user>
    <#if user??>
    <#assign whereclause = whereclause + "'" + user.id + "'" />
    <#if user_has_next><#assign whereclause = whereclause + "," /></#if>
    </#if>
    </#list>

    <#if wherclause??>
    <#assign messages = rest("2.0","/search?q=" + "SELECT subject, view_href, post_time_friendly, board.id, author.id FROM messages WHERE author.id IN (${whereclause}) ORDER BY post_time DESC LIMIT 5"?url) />


    <#list messages.data.items as recent >
    <#if recent??>
    <a href="${recent.view_href}">${recent.subject}</a><br/>
    </#if>
    </#list>
    </#if>

     

     

     

     

     

     

  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)

    bhupen 

     

    Looking at the documentation, I'm not sure /posts/latest takes any parameters (e.g. page_size and so on).

    A workaround would be to loop through your results and stop at 5 ...

    • bhupen's avatar
      bhupen
      Advisor

      OlivierS  Thanks for this. Can you share me any example or url where I can get reference to loop the api to stop it for 5 post??

      • OlivierS's avatar
        OlivierS
        Lithium Alumni (Retired)

        bhupen 

         

        Actually, while a loop should work, it might be best to use the Lithium API v2 and LiQL.

        Use the LIMIT keyword in your query to return the number of wanted messages, and sort them by time to get the latest ones. Something like:

         

        SELECT * FROM messages WHERE author.id='<user_id>' ORDER BY post_time ASC LIMIT 5

         

        I haven't tried it but should work ...