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 field not working on index.hbs

  • #6923
    Avatar photo[anonymous]

    Hi,
    in the Artgallery theme (override) in the config.json file I have created a new Subtitle field, like this:

    “postConfig”: [
    {
    “name”: “subTitle”,
    “label”: “Sottotitolo”,
    “value”: “”,
    “type”: “textarea”
    },

    Then in the post.hbs page I inserted:

    <p class=”p-class”>{{@config.post.subTitle}} <p>.

    It works fine

    But in the index.hbs page it doesn’t work!

    I followed this guide: https://getpublii.com/dev/post-config-options/

    and tried:

    <p>{{postViewConfig.subTitle}} <p>.

    <p>{{@postViewConfig.subTitle}} <p>

    {{#if postViewConfig.subTitle}}
    {{title}}
    {{/if}}

    none of the 3 tests worked.
    What’s wrong?

    Thanks

    #6927
    Avatar photo[anonymous]
    [anonymous] wrote:

    Hi,
    in the Artgallery theme (override) in the config.json file I have created a new Subtitle field, like this…

    Looks like this option is intended for posts template (post.hbs), so perhaps not the home page template (index.hbs).

    #6930
    Avatar photoBob

    The post config option is strictly related to the post. It means that it will display, also on the homepage but for the post, it is assigned to.
    So let’s see how it can work within the index.hbs file:

    the loop displaying the posts listing with title and excerpt:

    {{#each posts}}
       <article class="feed__item">
          <header>         
             <h2>
                <a href="{{url}}" >
                   {{title}}
                </a>
             </h2>
          </header>   
          <p>
            {{{ excerpt }}}
          </p>
       </article>
    {{/each}}

    now let’s add the contents of the subTitle field between the title and excerpt section using the postViewConfig object.

    {{#each posts}}
       <article class="feed__item">
          <header>         
             <h2>
                <a href="{{url}}" >
                   {{title}}
                </a>
             </h2>
          </header>
          <p> 
            {{postViewConfig.subTitle}}
          </p>   
          <p>
            {{{ excerpt }}}
          </p>
       </article>
    {{/each}}

    As a result, the subTitle will be displayed only for the post where you fill in the content of the subTitle field.

    For example, if the loop displays five posts and you fill in the subTitle field for only one of them, for example Post-X, then the subTitle will only be displayed once in the loop in place of Post-X.
    The rest of the posts in the loop will show an empty paragraph – to avoid it you can use the “if” statement:

    {{#if postViewConfig.subTitle}}
       <p>{{postViewConfig.subTitle}} </p>
    {{/if}}
    #6936
    Avatar photo[anonymous]

    Thank you very much for the clarification and the examples, I did a test and it works very well.
    Thanks again for your help.