How can you list the latest articles or products under a specified category in AnQiCMS?

Manage website content in AnQiCMS, often needing to display relevant information according to different topics or sections.For example, you may want to display the latest articles under the "Company News" category in some area of the homepage, or show the latest products under the "Hot New Products" category in the sidebar of the product page.AnQiCMS provides flexible and powerful template tags that allow you to easily achieve these content call requirements.

Understanding the content organization method of AnQiCMS is the key to efficiently utilizing its functions.The system distinguishes different types of content through 'content model', such as article model and product model.Under each content model, multiple "categories" can be created to further refine the content attribution.When you publish articles or products in the AnQiCMS backend, you need to assign them to specific content models and categories.This structured content management lays a solid foundation for our subsequent precise content calls through template tags.

We need to use one of the core template tags of AnQiCMS to list the latest articles or products in the specified category:archiveListThis tag is like an intelligent filter that can help us extract data that meets specific conditions from a massive amount of content.

archiveListCore parameter interpretation of the tag

In order to accurately call the latest content under the specified category, we need to masterarchiveListSeveral key parameters of the tag:

  1. moduleId: Specify the type of contentThis parameter is used to inform the system whether you want to call an article or a product.AnQiCMS defaults to having an article model (usually ID 1) and a product model (usually ID 2).You can view the specific ID of each model in the "Content Management" -> "Content Model" in the background.For example, if you want to list articles, you can setmoduleId="1"; If you want to display products, set it tomoduleId="2".

  2. categoryId: Precise classification positioningThis is the core parameter for implementing the 'specified category' call requirement.You need to provide the unique ID of the target category. This ID can be found in the AnQiCMS backend under "Content Management" -> "Document Category".When editing a category, you usually see the ID of that category in the URL or at some location in the editing interface.For example, if the ID of the "Company News" category is 10, then you can set the parameter tocategoryId="10".

  3. order: Sort rules to ensure "latest"To ensure that the content is "latest", you need to specify an appropriate sorting method. The two most common options are:

    • order="id desc": Arrange the content by ID in descending order, usually the larger the ID, the newer the content.
    • order="createdTime desc": Arrange according to the creation time in descending order, which can most accurately reflect the content's 'newness'. You can choose one according to your actual needs.
  4. limitControl the number of displayed items.This parameter is used to limit the number of items displayed at one time. For example, if you want to display only 5 of the latest articles, you can setlimit="5".

  5. type: List display form typeThe parameter has two main values:

    • type="list"Used for simple content list display, no pagination involved.
    • type="page"Used when you want the content list to support pagination, needs to be used withpaginationTags are used together.

Practice: Retrieve the latest articles or products under a specified category

Suppose we want to display the 5 latest articles under the 'Company News' category in a certain area of the website.

Step 1: Confirm the Category ID and Model ID

  1. Log in to the AnQiCMS backend, go to “Content Management” -> “Content Model”, confirm the ID of the “Article Model” (assuming it is1)
  2. Enter "Content Management" -> "Document Category", find the "Company News" category, and note its ID (assuming it is10)

Step 2: Write the template code

In your AnQiCMS template file (for example, the home page'sindex/index.htmlor the sidebar'spartial/sidebar.htmlYou can write code like this in Chinese:

<div class="latest-news-section">
    <h2>公司新闻</h2>
    <ul>
        {# 使用 archiveList 标签调用指定分类下的最新文章 #}
        {# moduleId="1" 表示调用文章模型的内容 #}
        {# categoryId="10" 表示只调用ID为10的“公司新闻”分类下的内容 #}
        {# order="createdTime desc" 确保内容按发布时间降序排列,即最新内容在前 #}
        {# limit="5" 限制只显示最新的5篇文章 #}
        {% archiveList latestPosts with moduleId="1" categoryId="10" order="createdTime desc" limit="5" %}
            {% for item in latestPosts %}
                <li>
                    <a href="{{ item.Link }}" title="{{ item.Title }}">
                        {# 如果文章有缩略图,可以显示出来 #}
                        {% if item.Thumb %}
                            <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="news-thumb">
                        {% endif %}
                        <span class="news-title">{{ item.Title }}</span>
                        {# 格式化文章发布时间 #}
                        <span class="news-date">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                    </a>
                </li>
            {% empty %}
                {# 当该分类下没有内容时,显示此提示 #}
                <li><p>该分类下暂无最新文章。</p></li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

This code will traversearchiveListReturn the latest list of articles. For each article (itemWe can go through the}item.LinkGet its link,item.TitleGet the title,item.ThumbRetrieve thumbnails,item.CreatedTimeGet publish time