When operating a website, we often need to highlight certain important or recommended content, such as placing the latest news on the 'top news' or marking popular products as 'recommended'.AnQiCMS provides a flexible recommendation feature that allows you to easily filter and display these specific contents, thereby better guiding users to browse and enhance content value.
Understand the recommended attributes and their functions
AnQiCMS 中的 recommended attribute, which can be regarded as a special tag of content, used to mark its importance and display method on the website. These attributes are diverse, such as:
- Headline [h]:通常用于网站最重要、最受关注的内容。
- Recommend [c]:泛指需要向用户特别推荐的内容。
- Slideshow [f]:常用于轮播图或焦点图区域的内容。
- [en] Recommended [a]:Content with special value or long-term recommendation.
- [en] Scroll [s]:Content suitable for display in scrolling areas (such as bulletin boards).
- [en] Image [p]:Indicates that the content contains important images, suitable for image list display.
- [en] Jump [j]:Click to jump directly to the external link instead of the content detail page.
These properties are not mutually exclusive, you can select multiple recommended properties for a document at the same time in the background.For example, an important article can be set as both 'Top News' and 'Image'.By flexibly using these properties, you can make the website content show more rich layers and dynamics.
Set recommended attributes in the AnQiCMS background:
When you create or edit documents in the background, you will find a setting item named "Recommended Properties".Here is an example of an article, where you enter the interface for 'Publish Document' or edit an existing document under the 'Content Management' module.In the middle of the document editing interface, there is usually an 'Suggested Properties' selection area.You can select one or more corresponding properties based on the importance of the content.After saving the document, these properties will take effect, waiting to be called in the frontend template.
Call specific recommended content in the template
To display these content with specific recommendation properties on the website frontend, we need to use the AnQiCMS provided template files.archiveListLabel. This label function is powerful and can filter and output content lists based on multiple conditions.
The key to filtering content based on recommended attributes isarchiveListTagsflagParameter. You only need to assign the letter code of the required recommended attributeflagto the parameter. For example, if you want to display "Top Stories" content, set it toflag="h"If you want to display 'Recommended' content, set it toflag="c".
Exceptflagparameters, you can also combine other parameters to more precisely control the display of content:
moduleId: Specify the model ID of the content. For example,moduleId="1"usually represents an article model,moduleId="2"Represents a product model. This ensures that you are filtering specific types of content.categoryId:Specify the category ID of the content. If you want to display recommended content under a specific category, you can use this parameter. For example,categoryId="3"Only recommended contents under the category with ID 3 will be displayed.limit: Control the number of displayed contents. For example,limit="5"Up to 5 contents will be displayed at most.order:Define the sorting method of the content. Common options includeid desc(Sorted by publish time in reverse order, i.e., the most recent published),views desc(Sorted by view count in reverse order, i.e., the most popular).
It should be noted that in a singlearchiveListin the tag,You can only use one recommended attribute for filtering at a time.If you need to display the content of multiple recommended attributes, you can use multiple ones separately.archiveListtags to call.
Template code examples and explanations
The following demonstrates how to filter and display the content list based on recommended attributes in the AnQiCMS template through two specific examples.
Example one: Display the latest 5 'Top Stories' articles on the homepage
Assume that your website homepage has a "Latest Headlines
{# 首页热门头条文章区域 #}
<div class="homepage-headlines">
<h2>最新头条</h2>
<ul class="article-list">
{% archiveList headlines with moduleId="1" flag="h" limit="5" order="id desc" %}
{% for item in headlines %}
<li class="article-item">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p class="description">{{ item.Description|truncatechars:100 }}</p> {# 截取前100个字符作为简介 #}
<span class="publish-date">发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
</li>
{% empty %}
<li class="no-content">暂无头条文章可供显示。</li>
{% endfor %}
{% endarchiveList %}
</ul>
</div>
Code analysis:
{% archiveList headlines with ... %}This is the core tag,headlinesThe variable name is defined for the content list you have obtained.moduleId="1": Specify the content of the article model we want to obtain (assuming the article model ID is 1).flag="h":Precisely filter out all content marked as 'Top News'.limit="5":Limit to displaying up to 5 articles.order="id desc":According to the article ID in descending order, it usually means displaying the latest published articles.{% for item in headlines %}: Iterate overheadlineseach article in the variable.{{ item.Link }}:Get the article link.{{ item.Title }}:Get the title of the article.{{ item.Description|truncatechars:100 }}:Get the description of the article and usetruncatecharsthe filter to extract the first 100 characters.{{ stampToDate(item.CreatedTime, "2006-01-02") }}:The article creation timestamp is formatted into a string of 'year-month-date'.{% empty %}:This is a very practical tag, whenarchiveListNo content matching the criteria is found, it will display{% empty %}the content within the block, to avoid page blank or error.
Example two: Display 4 'Recommended' products in the product category page sidebar
假设您的某个产品分类页面侧边栏,需要展示 4 款被标记为“Recommended”的产品,并且这些产品应属于当前分类,按浏览量从高到低排序。
`twig {# Product category page sidebar recommended products #}