How to filter and display a document list based on specific recommendation attributes (such as "Top Stories

In AnQiCMS, the flexible application of content recommendation attributes is a key link to enhance the exposure and user experience of website content.Whether you want to highlight important news on the homepage or recommend selected content in a specific section, AnQiCMS provides a convenient and efficient solution.This article will delve into how to filter and display document lists based on specific attributes such as "Top Stories" and

Part 1: Understanding Recommendation Attributes and Their Settings

In the AnQiCMS content management system, recommended properties are a very practical feature that allows us to mark documents with different importance levels or display styles.These properties usually include but are not limited to 'Headline', 'Recommendation', 'Slider', etc., each corresponding to a brief letter identifier for easy calling within the template.

Specifically, AnQiCMS provides the following commonly used recommendation properties:

  • Headline [h]: Typically used for the most important and most highlighted content on the website.
  • Recommend [c]: Indicates that the content is recommended for editing, suitable for display in lists or sidebars.
  • Slideshow [f]: Content designed specifically for carousels or featured images.
  • [en] Recommended [a]: An English translation for 'auto' would be 'A special recommendation that may have a higher priority.'
  • [en] Scroll [s]: Suitable for content displayed in a scrolling manner on a page, such as a bulletin board.
  • 加粗 [English]: Textual emphasis, but may also be used for content filtering in some template designs.
  • [en] Image [p]: Emphasized content includes important images, may be used for image lists.
  • [en] Jump [j]: 指示该内容点击后会跳转到外部链接或指定页面。

How to set recommended properties in the background?

When you publish or edit a document, you will see a section named "Recommended Properties" prominently displayed in the document editing interface.Here, you can select one or more recommended properties based on the actual needs of the document.For example, if a news article is a major highlight on the current website, you can check 'Headline[h]' and 'Recommend[c]'.AnQiCMS allows you to select, deselect, or multi-select these properties, which lays a foundation for flexible front-end content display.

It should be noted that although multiple recommended properties can be selected for the document in the background, it is usually for a single property when calling the front-end template tags.

Part two: UsingarchiveListTags for filtering and display

AnQiCMS's template system uses syntax similar to Django, wherearchiveListTags are the core tools for retrieving document lists. By usingarchiveListThe parameters of the label, we can easily realize the need to filter documents by recommended attributes.

Basic filtering: Get documents with specific recommended attributes

To filter documents with specific recommendation attributes, we mainly usearchiveListTagsflagParameter.flagThe value of the parameter is the letter identifier we see when we set the recommendation attributes in the background.

假设我们想获取带有“头条”属性的最新5篇文章,并以列表形式展示,可以这样编写模板代码:

{# 获取带有“头条”属性 (flag="h") 的最新5篇文档 #}
<div class="headlines-section">
    <h3>头条新闻</h3>
    <ul>
        {% archiveList archives with flag="h" limit="5" order="id desc" %}
            {% for item in archives %}
                <li>
                    <a href="{{ item.Link }}" title="{{ item.Title }}">
                        <h4>{{ item.Title }}</h4>
                        <p>{{ item.Description|truncatechars:80 }}</p>
                        <span>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                        <span>浏览量:{{ item.Views }}</span>
                    </a>
                </li>
            {% empty %}
                <li>暂无头条新闻。</li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

In this example:

  • flag="h":指定只筛选带有“头条”属性的文档。
  • limit="5":Limit to displaying only 5 articles.
  • order="id desc":按照文档ID降序排列,通常代表最新发布的文档。
  • item.Link/item.Title/item.Description/item.CreatedTime/item.ViewsIt is a commonly used field in the document object, and you can choose to display it as needed.stampToDateIt is a practical time formatting function.
  • truncatechars:80It is a filter used to truncate the description content to prevent it from being too long.

Advanced Filtering and Pagination: Combine with other parameters and pagination features

We can not only filter according to recommended attributes, but also combine classification, content models, etc. for more fine-grained filtering, and achieve pagination display.

We want to display all 'recommended' documents under a specific category (for example, the article category with ID 10) and support pagination, with 10 documents displayed per page:

{# 获取分类ID为10下,带有“推荐”属性 (flag="c") 的文档,并分页显示 #}
<div class="recommended-articles">
    <h3>推荐文章列表</h3>
    <ul>
        {% archiveList archives with categoryId="10" flag="c" type="page" limit="10" order="id desc" %}
            {% for item in archives %}
                <li>
                    <a href="{{ item.Link }}" title="{{ item.Title }}">
                        {% if item.Thumb %}<img src="{{ item.Thumb }}" alt="{{ item.Title }}">{% endif %}
                        <h4>{{ item.Title }}</h4>
                        <p>{{ item.Description|truncatechars:100 }}</p>
                        <span>{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                    </a>
                </li>
            {% empty %}
                <li>当前分类下暂无推荐文章。</li>
            {% endfor %}
        {% endarchiveList %}
    </ul>

    {# 分页导航 #}
    <div class="pagination-section">
        {% pagination pages with show="5" %}
            {# 可以根据pages变量的属性,如pages.FirstPage.Link, pages.Pages等,构建完整的分页导航 #}
            {% if pages.PrevPage %}<a href="{{ pages.PrevPage.Link }}">上一页</a>{% endif %}
            {% for page in pages.Pages %}<a class="{% if page.IsCurrent %}active{% endif %}" href="{{ page.Link }}">{{ page.Name }}</a>{% endfor %}
            {% if pages.NextPage %}<a href="{{ pages.NextPage.Link }}">下一页</a>{% endif %}
        {% endpagination %}
    </div>
</div>

In this example:

  • categoryId="10":The document must belong to the category with ID 10.
  • flag="c":Specify the "recommended" attribute for filtering.
  • type="page":Enable pagination feature, so thatarchiveListThe label will return the data required for pagination, rather than returning all results at once.
  • limit="10": Displaying 10 documents per page.
  • {% pagination pages with show="5" %}: Combined with the pagination label, it will automatically according toarchiveListThe label returns pagination data to generate pagination links.

Excludes documents with specific properties.

In addition to filtering specific attributes, sometimes we may need to exclude documents with certain attributes.For example, in a conventional article list, we may not want to display documents marked as "Headline" to avoid repetitive content display or to disperse focus.excludeFlagParameter.

{# 获取所有文章,但排除掉“头条” (excludeFlag="h") 文档 #}
<div class="all-articles">
    <h3>最新文章 (排除头条)</h3>
    <ul>
        {% archiveList archives with moduleId="1" type="list" limit="10" order="id desc" excludeFlag="h" %}
            {% for item in archives %}
                <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
            {% empty %}
                <li>暂无文章。</li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

HereexcludeFlag="h"Ensures that the list of articles obtained does not contain any documents marked with the 'Top News' property.

Show recommended attribute identifiers in document list

If you want to directly display the recommended attributes of documents in the document list,archiveListtag.showFlag=trueParameter.

`twig {# 获取文章并显示其推荐属性标识 #}