【问题标题】:Bolt CMS - List each record for taxonomy optionBolt CMS - 列出分类选项的每条记录
【发布时间】:2017-12-10 19:47:36
【问题描述】:

我正在尝试获取每个分类选项的列表,并在每个选项下方列出使用该分类选项的记录。

这是我想要的最终结果:

家人/朋友/家
诗 1
诗 2
卡特
诗 3
诗 4
诗 5
政治
诗 6
诗 7

这是taxonomy.yml:

poems:
    name: Poems
    slug: poems
    singular_name: Poem
    singular_slug: poem
    behaves_like: categories
    options: { familyfriendshome: "Family/Friends/Home", cath: "Cath", politics: "Politics", observations: "Observations", whimsical: "Whimsical", other: "Other" }
    has_sortorder: true

这是 contenttypes.yml

poems:
    name: Poems
    singular_name: poem
    behaves_like: categories
    fields:
        slug:
            type: slug
            uses: name
        name:
            label: Title of Poem
            type: text
            placeholder: e.g., The Way You Look Tonight
        sortname:
            label: Name for Sorting
            type: text
            placeholder: This won't be visible on the site - it's just for sorting
        date:
            label: Date written
            type: text
            placeholder: e.g., August 2017
        text:
            type: html
            height: 300px
            #options: 
                #ckeditor: 
                    #removeButtons: 'Format,Styles,Font,Outdent,Indent,RemoveFormat,Source'
    listing_template: poemsTEST.twig
    record_template: poemdetail.twig
    taxonomy: [ poems ]
    file_under: poems
    recordsperpage: 300
    default_status: published

这是我目前在列表模板中得到的:

<ul>
    {% for poem in app.config.get('taxonomy/poems/options') %}

        <li>{{ poem }}

        {% setcontent allpoems = 'poems' where { options: 'cath' } printquery %}
            <ul>

                {% for poem in allpoems %}
                    <li>
                        <a href="{{ poem.link }}">{{ poem.title }}</a>
                    </li>
               {% endfor %}

        </li>

            </ul>

    {% endfor %}
</ul>

这是它产生的:

家人/朋友/家
诗 1
诗 2
诗 3
诗 4
诗 5
诗 6
诗 7
卡特
诗 1
诗 2
诗 3
诗 4
诗 5
诗 6
诗 7
政治
诗 1
诗 2
诗 3
诗 4
诗 5
诗 6
诗 7

我尝试设置选项:cath 最初只是为了看看它是否会选择正确的记录。如果我向其中添加 printquery,我会得到:

SELECT bolt_poems.* FROM bolt_poems WHERE ("bolt_poems"."status" = 'published') ORDER BY datepublish DESC LIMIT 9999

它会列出每条记录并忽略选项。

我在问不可能吗?

提前致谢

Cath

附言我以前应该注意到的东西。后端诗歌概览页面具有我想要的布局(在 taxonomy.yml 中使用 has_sortorder: true) - 分类的名称,然后是使用该分类的所有诗歌

【问题讨论】:

    标签: twig taxonomy bolt-cms


    【解决方案1】:

    在您的 setcontent 中,您可以直接使用分类法“诗歌”。

    <ul>
    {% for poem_taxonomy_term in app.config.get('taxonomy/poems/options') %}
        <li>{{ poem_taxonomy_term }}
        {% setcontent allpoems = 'poems' where { poem: poem_taxonomy_term } printquery %}
            <ul>
                {% for poem_record in allpoems %}
                    <li>
                        <a href="{{ poem_record.link }}">{{ poem_record.title }}</a>
                    </li>
               {% endfor %}
            </ul>
        </li>
    {% endfor %}
    </ul>
    

    【讨论】:

    • 非常感谢@Jadwigo 看到这个,但它给了我相同的结果。我得到了分类项目,每个项目下面都列出了每首诗(不管它使用哪种分类)。
    • 啊,我无法在真正的螺栓安装上测试这个,所以这就解释了为什么它不起作用。您必须将 setcontent 行更改为 {% setcontent allpoems = 'poems' where { poems: poem_taxonomy_term } printquery %},因为它必须是分类法的复数:docs.bolt.cm/3.4/templating/content-fetching#using-taxonomies
    【解决方案2】:

    哦,快乐。 #3儿子来救援。这几乎是完美的 - 列出了每个分类法,只有使用该分类法的诗歌。仍有待完成 - 如果没有使用分类法(观察和其他),它仍然会出现(下面没有诗歌)。

    <ul>
      {% for poem_taxonomy_term, poem_taxonomy_desc in app.config.get('taxonomy/poems/options') %}
          <li>{{ poem_taxonomy_desc }}
          {% setcontent allpoems = 'poems' %}
              <ul>
                  {% for poem_record in allpoems %}
                    {% if (poem_record.taxonomy['poems']['/poems/' ~ poem_taxonomy_term]) %}
    
                      <li>
                          <a href="{{ poem_record.link }}">{{ poem_record.title }}</a>
                      </li>
                    {% endif %}
                 {% endfor %}
              </ul>
          </li>
      {% endfor %}
    </ul>
    

    @jadwigo - 这是我应该能够从文档中解决的问题吗?

    【讨论】:

    • 文档中没有这样的解决方案...您需要稍微优化一下。当您有很多诗歌时,它将为每个分类选项再次加载所有诗歌。如果将行 {% setcontent allpoems = 'poems' %} 移动到第一个 &lt;ul&gt; 之前,它将只加载一次。在{% if (poem_record.taxonomy['poems']['/poems/' ~ poem_taxonomy_term]) %} 行的记录中检查现有分类法非常紧凑。如果您在 config.yml 中启用 strict_variables: true,此行将破坏您的站点。在小型网站上使用可能没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多