Download
We're evolving to serve you better! This current forum has transitioned to read-only mode. For new discussions, support, and engagement, we've moved to GitHub Discussions.

Custom loops with pagination in the homepage

  • #4816
    Avatar photo[anonymous]

    Hello everyone, new user here.

    I started out with publii few days ago, and I’m trying make my custom theme starting from the free examples on your site.

    What I want to achieve: having multiple sections on the homepage, one let’s say for `type1` and one for `type2`. In my case the type of the content is defined by the tag or by the template (not important for the moment). Each section only displays a fixed number of posts for each type, and in each section there is a button that implements either pagination, or if this is not possible, takes to a separate page that displays all the posts of that type.

    A little background: from what I understand (please correct me If I am wrong) publii exposes the array `posts`. The content of the array <span style=”text-decoration: underline;”>depends on the context,</span> more specifically depends on the template I am working in: so for example if working in the `tag.hbs` template, the content of `posts` will only be posts of a specific tag. If working in `index.hbs`the contents will be all the types of posts, the number of which depends on the maximum number of posts that was set for that page.

    By using this method, and adding the `{{pagination}}` handle in the template, publii then does his magic and generates the correct amount of posts per page, the button, and all the pages `mywebsite.com/page/n/`

    <span style=”text-decoration: underline;”>The problem</span>: I can’t manage to get custom content with pagination, I have tried different approaches

    1. In the main loop, my first try was: `{{#each posts}} {{#checkIf template ‘==’ ‘type1’}} … {{/checkIf}} {{/each}}` this works regarding the content printed but (obviously, it’s a stupid attempt I know, but again, I just started) it leaves ‘holes’ because the number of iterations per page if fixed
    2. I then tried to query the posts myself: `{{#getPostsByTags “count=9&excluded=hidden&tag_as=id&tags=6&orderby=createdAt&ordering=desc”}}` this again, retrieves the content I want but, pagination won’t work with it (it seems it only applies to loop on the `posts` array) and if a pagination element in on the page, the content will just be repeated on the new page.
    3. I found the guide https://getpublii.com/dev/how-to-display-posts-connected-with-a-specific-tag-name/ which basically does the same as point number 2.

    At this point it seems like the only solution would be to give up pagination, just show a fixed number of more-recent-posts using the second/third method and add a link-button to a tag page if the reader wants to see more contet.

    So my question is: am I missing something? Is there another, standard, way of doing this?

    Thanks to everyone

    #4817
    Avatar photoBob

    Pagination refers to the total number of posts in the loop, so it does not display the fixed number of pages within the post template.
    The {{getPostsByTags}} helper does not support pagination, so the best option for you is to display the link to the tag page with {{getTag}} helper https://getpublii.com/dev/gettag-helper/

    If you decide to display the posts with the {{getPostsByTags) helper, you can use a Tags Dropdown indicator https://getpublii.com/dev/tags-dropdown/ for both helpers to automatically generate the posts and link to the tag page.

    {{getPostsByTags}}

    {{#getPostsByTags (concatenate "count=" @config.custom.amountOfPosts "&allowed=hidden,featured&tag_as=id" "&tags=" @config.custom.TagsDropdown
     "&excluded=1,2&offset=1&orderby=modifiedAt&ordering=asc")}}  
        <h2>{{ title }}</h2> 
        <div>{{{ excerpt }}}</div> 
    {{/getPostsByTags}}

    {{getTag}}

    {{#getTag @config.custom.TagsDropdown}}
       <a href="{{url}}"> link to the tag page</a>
    {{/getTag}}

    Tag dropdown (config.json)

    {
       "name": "TagsDropdown",
       "label": "Select tags",
       "group": "Frontpage",
       "note": "Display posts from the selected tag/s",
       "value": "",
       "type": "tags-dropdown",
       "multiple": true
    },
    #4819
    Avatar photo[anonymous]

    Thank you very much for the fast reply! Very useful.

    I will start working on it and maybe I’ll be back with some updates in case I can contribute something useful for other readers.

    #4854
    Avatar photo[anonymous]

    Hello,

    So, I’ve been working on the different categories using the `#getPostsByTag` as suggested, and for the home page it works great. But I have a problem which seems to me, given the current features of publii, cannot be solved (maybe it’s beyond the scope of Publii) without changing the code of the renderer.

    So let’s say I have category1 (`cat1`) and category2 (`cat2`) identified by 2 different tags `tag1` and `tag2`. Now my two different categories have very different preview styles so that they can’t be mixed together in the same post list. `cat1` is displayed in grid style, `cat2` is displayed as a list (1 column, n rows).

    Let’s say I have `tag3` that is shared both by `cat2`and `cat1`. If I go to the tag page `../tags/tag3` the same exact problem as in the home page appears: the result are mixed together, and I can’t have pagination set up properly without having ‘holes’ in the grid: if I have set 9 posts for the `tag3` page, the results will be split like `numberof cat1` + `numberof cat2` = 9

    The result I would like to achieve is for each category to have a set amount of results per page, and to have pagination. Is it possible possible to do so in some other way?

    P.s. sorry for the formatting of the messages here on the blog, I have used the `code` tag  in my first message but for some reason it didn’t render it.

    #4855
    Avatar photoBob

    The tag page displays the number of posts defined in the theme’s basic settings section. There is no way to display a different number of posts on different tag pages. You can have a different tag page layout (by tags custom template) but the total number of posts always is the same.