Download

{{#getPosts}} helper

{{getPosts}} is a helper used to retrieve post data regardless of the current context.

Syntax:

{{#getPosts "postID1,postID2,postID3" "before" "after"}}
...
{{/getPosts}}

Example:

{{#getPosts "POST_ID_1,POST_ID_2,POST_ID_N" "<ul>" "</ul>"}}
<li>
    <h2>{{ title }}</h2>
    <div>{{{ excerpt }}}</div>
</li>
{{/getPosts}}

Posts are ordered by the ID order in the string.

You can also use the @index variable to detect the post number in the specified collection:

{{#getPosts "1,2,3" "<ul>" "</ul>"}}
<li>
    <h2>{{ title }}</h2>
    <div>{{{ excerpt }}}</div>
    {{#checkIf @index '==' 0}}
    <p>IT'S THE FIRST ELEMENT!</p>
    {{/checkIf}}
</li>
{{/getPosts}}

The second parameter creates an HTML prefix, and the third parameter creates an HTML suffix for the generated output.

Additionally, it is possible to enhance the dynamic fetching and display of posts by utilizing the posts dropdown control from the Theme Settings API, in conjunction with the concatenate helper to combine query string options with theme option values. This control allows users to select specific posts to be displayed. Integrating this functionality within your theme involves directly referencing the selected posts through the theme configuration. For instance, if you have a posts dropdown control named postsDropdown in your Theme Settings, its value can be dynamically used with the getPosts helper, as shown below:

{{#getPosts (concatenate  @config.custom.postsDropdown) "<ul>" "</ul>"}}
    <li>
        <h2>{{ title }}</h2>
        <div>{{{ excerpt }}}</div>
    </li>
{{/getPosts}}

In this setup, @config.custom.featuredPostsDropdown directly refers to the comma-separated post IDs the user selects through the dropdown control. This approach ensures that the displayed posts can be dynamically controlled via theme settings, offering a high degree of customization for theme users.

IMPORTANT: It requires availability of the @website.contentStructure global variable

What are you waiting for?

Start building your site today.

  1. 1 Download Publii
  2. 2 Write your content
  3. 3 Publish your site
Create website