In AnQiCMS, the flexible application of content recommendation properties 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 specific sections, AnQiCMS provides convenient and efficient solutions.This article will delve into how to filter and display document lists based on specific attributes such as "Headlines", "Recommendations", and others, helping you better manage and present website content.
Part One: Understanding Recommendation Attributes and Their Settings
In the AnQiCMS content management system, the recommended attribute is a very practical feature that allows us to mark documents with different importance levels or display methods.These properties typically include but are not limited to 'headline', 'recommendation', 'slideshow', etc., each corresponding to a short letter identifier for easy use in templates.
In particular, AnQiCMS provides the following commonly used recommendation properties:
- Headline [h]: Usually 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.
- Slide [f]: Content designed specifically for carousels or focus images.
- Special recommendation [a]: A special recommendation, which may have a higher priority.
- Scroll [s]: Suitable for displaying content in a scrolling manner on a page, such as a bulletin board.
- Bold [h]Text-level emphasis, but may also be used for content filtering in some template designs.
- Image [p]Emphasized content includes important images, may be used in image lists.
- Jump [j]: Indicates that clicking on this content will jump to an external link or specified page.
How to set recommended attributes in the background?
When you publish or edit a document, you will see a section called "Recommended Properties" prominently displayed in the document editing interface.Here, you can check one or more recommended properties based on the actual needs of the document.For example, if an article is a major content on the current website, you can check 'Headline[h]' and 'Recommend[c]'.AnQiCMS allows you to select, multi-select, or not select these properties, which lays the foundation for flexible display of front-end content.
It should be noted that although the background can select multiple recommended properties for the document, when calling the front-end template tags, it will usually filter for a single property.
Part two: usearchiveListTags for filtering and display
AnQiCMS template system adopts a syntax similar to Django, wherearchiveListTags are the core tools for obtaining document lists. By using them flexibly,archiveListThe label's parameters, we can easily implement the need to filter documents by recommended attributes.
Basic filtering: Get documents with specific recommended attributes.
To filter documents with specific recommendation properties, we mainly usearchiveListlabel'sflagParameter.flagThe value of the parameter is the letter identifier we see when setting recommendation properties in the background.
Suppose we want to get the latest 5 articles with the 'headline' attribute and display them as a list, we can write the template code like this:
{# 获取带有“头条”属性 (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"Specify only the documents that have the 'Top News' attribute.limit="5":Limit to displaying only 5 articles.order="id desc"Arrange the documents in descending order by document ID, usually representing the most recently published documents.item.Link/item.Title/item.Description/item.CreatedTime/item.ViewsIt is a commonly used field in the document object, 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: combining other parameters and pagination features
We can not only filter according to recommended attributes, but also combine classification, content models, and other fine-grained filtering, and implement pagination display.
Suppose 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 filter.type="page": Enable pagination function, so thatarchiveListThe tag will return the data needed for pagination, rather than returning all results at once.limit="10": Displays 10 documents per page.{% pagination pages with show="5" %}: Combined with the pagination tag, it will automatically according toarchiveListThe label returns the pagination data to generate pagination links.
Exclusion of documents with specific attributes
In addition to filtering specific attributes, sometimes we may need to exclude documents with certain attributes.For example, in a regular article list, we may not want to display documents that have been marked as "headline" to avoid repetitive content display or to分散 focus.This can be used at this timeexcludeFlagParameter.
{# 获取所有文章,但排除掉“头条” (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"Will ensure that the list of articles obtained does not contain any documents marked with the "headline" attribute.
Display recommended property identifier in document list
If you want to display the recommended properties of the document directly in the document list, you canarchiveListthe tag withshowFlag=trueParameter.
"`twig {# Retrieve the article and display its recommended attribute identifier #}