How to loop through custom parameters of articles in a template (such as author, source)?

Calendar 👁️ 61

In Anqi CMS, flexibly displaying customized article parameters is the key to enhancing the personalization and richness of website content.In order to better describe product features, or to clearly mark the author and source in the article, custom parameters can provide strong support.The Anqi CMS provides a concise and efficient way to define and call these parameters, allowing you to easily display them in website templates.

Next, we will discuss step by step how to implement the loop display of custom parameters in the Anqi CMS template.

Step 1: Understand and create custom parameters

In Anqi CMS, custom parameters are defined based on the 'content model'.This means you can set exclusive parameters for different types of content (such as articles, products, etc).For example, the article model can have parameters such as 'author' and 'source', while the product model can have parameters such as 'size' and 'color'.

To create these custom parameters, you need to set them in the Anqi CMS backend:

  1. Log in to the backend and navigate toContent Management>Content model.
  2. Select the content model you want to add custom parameters to (such as "article model"), click the "Edit" button.
  3. On the content model editing page, find the 'Content Model Custom Field' section.
  4. Here, you can add new fields. Each field needs to set a 'parameter name' (for display in the background), a 'calling field' (this is the unique identifier you use in the template, recommended to use lowercase English letters), 'field type' (such as single-line text, multi-line text, single selection, etc.), whether it is required, and the default value.

For example, if you want to add a "source" parameter to an article, you can create a call field namedsourceThe parameter named "Article Source", a custom field of type "Single Line Text".

Step 2: Display the custom parameter on the article detail page.

After you have created and filled in the custom parameters for the article in the background, the next step is to call them in the front-end template. On the article detail page({模型table}/detail.htmlor{模型table}/detail-{文档ID}.html), AnQi CMS providesarchiveDetailTag to get the detailed information of the current article, which also includes custom parameters.

If you know the specific name of the custom parameter (for example,authororsource),can be called directly in the following way:

{# 假设您在后台创建了调用字段为 author 和 source 的自定义参数 #}
<div>作者:{% archiveDetail with name="author" %}</div>
<div>来源:{% archiveDetail with name="source" %}</div>

Or, if your template context already includes the current article object (usually on the article detail page and namedarchivevariable), you can also access it directly using dot notation:

<div>作者:{{archive.author}}</div>
<div>来源:{{archive.source}}</div>

This method is suitable when you know exactly which custom parameters you need to display.

Step 3: Loop through all the custom parameters of the article.

In some cases, you may want to dynamically display all the custom parameters of the article, or you are not sure what parameters there will be, and just want to list all the non-empty parameters that have been set. At this time,archiveParamsThe label comes into play. This label is specifically used to obtain all custom parameter lists of a specified article and supports looping.

archiveParamsThe usage method of the label is as follows:

{% archiveParams params %}
    {% for item in params %}
    <div>
        <span>{{item.Name}}:</span>
        <span>{{item.Value}}</span>
    </div>
    {% endfor %}
{% endarchiveParams %}

In the code above:

  • {% archiveParams params %}Assigns the collection of all custom parameters of the current article toparamsVariable.
  • {% for item in params %}Then it will traverse thisparamscollection, each time loop,itemThe variable represents a custom parameter.
  • {{item.Name}}The value is the parameter name set in the background (for example, "author", "source of article").
  • {{item.Value}}The value is the specific value of the parameter (for example, "Zhang San", "Xinhua News Agency").
  • If the value of a custom parameter may contain HTML content (such as using a multi-line text field), to ensure that HTML is parsed correctly and not displayed as plain text, you can{{item.Value}}added afterwards|safea filter such as{{item.Value|safe}}.

If you want to get the custom parameters of a specified article ID instead of the parameters of the article on the current page, you can use it like thisarchiveParams:

{% archiveParams params with id="123" %} {# 这里的123替换为具体的文章ID #}
    {% for item in params %}
    <div>
        <span>{{item.Name}}:</span>
        <span>{{item.Value}}</span>
    </div>
    {% endfor %}
{% endarchiveParams %}

Step 4: Display custom parameters on the article list page

On the article list page (for example, category list page, search results page, etc.), you will usually usearchiveListLabel to loop through multiple articles. If you want to display custom parameters in each article item, you can putarchiveParamsNested in a tagarchiveListinside the loop.

{% archiveList articles with type="page" limit="10" %}
    {% for article_item in articles %}
    <div class="article-item">
        <h3><a href="{{article_item.Link}}">{{article_item.Title}}</a></h3>
        <p>发布时间:{{stampToDate(article_item.CreatedTime, "2006-01-02")}}</p>

        {# 在这里调用当前文章的自定义参数 #}
        {% archiveParams custom_fields with id=article_item.Id %}
            {% for field in custom_fields %}
            <div>
                <span>{{field.Name}}:</span>
                <span>{{field.Value}}</span>
            </div>
            {% endfor %}
        {% endarchiveParams %}

        <p>{{article_item.Description}}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

In this example,archiveListin the looparticle_itemrepresent each article, we useid=article_item.IdPass the current article ID toarchiveParamsso that the custom parameters of the article can be retrieved and displayed.

Summary

The Anqi CMS custom parameter function combined with powerful template tags provides you with great flexibility, allowing you to accurately display additional information of articles at any location on the website according to actual needs. By making reasonable use ofarchiveDetailandarchiveParamsTags, whether it is a fixed parameter or a dynamic parameter list, can easily handle, making your website content more attractive and professional.


Frequently Asked Questions (FAQ)

1. I have set custom parameters in the background, but why can't I access them through the front-end template?{{archive.customFieldName}}This usually happens when?This usually happenscustomFieldName(The spelling of the 'call field' you set in the background is incorrect, or the current template context contains)archiveThe variable does not contain the custom parameter data. Please check the spelling of the called field and make sure you are on the article detail page orarchiveListinside a loop andarchiveThe variable indeed points to the correct article object. UsearchiveParamsTagging for iteration is a more robust method, as it explicitly returns all custom parameters that have been set.

How to display a specific custom parameter without looping through all parameters?If you want to display a known specific custom parameter, you can use it directly.{% archiveDetail with name="您的调用字段名" %}Label. For example, to display 'author', you can use{% archiveDetail with name="author" %}. This way avoids unnecessary loops, making the code more concise and clear.

3. My custom parameter is a multi-line text field that contains HTML code, how do I display these HTML contents correctly in the template?If the value of your custom parameter may contain HTML code (for example, a brief introduction with bold text and links), in order for the browser to parse these HTML correctly rather than displaying them as plain text, you need to use|safea filter. For example:<span>{{item.Value|safe}}</span>Please note that using|safeThe filter means you trust the content of the parameter to be safe, and it will not introduce malicious scripts.

Related articles

How does AnQiCMS display a list of recommended content related to the current article?

In content management and website operations, how to effectively attract visitors, extend their stay on the website, and guide them to discover more interesting content is a crucial issue.AnQiCMS (AnQiCMS) provides a powerful and flexible mechanism that helps us easily display a list of related recommended content on the current article page, thereby greatly enhancing the user experience and the overall performance of the website.**Core Mechanism: `archiveList` tag's `type="related"` attribute** AnQiCMS

2025-11-08

How to display the title and link of the 'Previous' and 'Next' articles on the article detail page?

In website content operation, the navigation design of the article detail page is crucial.A well-designed user experience detail page that can clearly present the current content and naturally guide readers to browse more related or coherent articles.The display of the 'Previous' and 'Next' articles is a classic approach to enhance user stickiness and the efficiency of the website's internal link structure.AnqiCMS is an enterprise-level content management system developed based on the Go language, which fully considers the efficiency and flexibility of content management from the beginning of its design.It relies on a concise and efficient architecture

2025-11-08

How does AnQiCMS's 'Time Factor - Scheduled Publishing' feature affect the public display of content?

In the era where content is king, the timing and rhythm of website content release often determines its propagation effect and user reach rate.For content operators, manually launching content at every important moment is not only time-consuming and labor-intensive, but also prone to missing the opportunity due to carelessness.AnQiCMS knows this pain point and has specially introduced the "Time Factor-Scheduled Publishing" feature, aiming to make the content launch process more intelligent and accurate, providing users with a more relaxed content management experience.Understanding the operation mechanism of "Time Factor-Scheduled Publishing" In the AnQiCMS management background

2025-11-08

How to retrieve and display detailed information of a specific category, such as the category introduction and Banner image?

In website operation, clear and attractive categorization information is crucial for improving user experience and optimizing search engine performance.A well-designed category page can not only help users quickly understand the theme of the category, but also attract users by displaying relevant visual elements (such as banner images) and provide rich content for SEO.Strong and flexible features provided by AnQi CMS, making it easy to achieve these goals. ### Configure Category Information in AnQi CMS Backend Managing detailed category information in AnQi CMS is very intuitive.Log in to the backend management interface after

2025-11-08

How does AnQiCMS's 'Document Parameter Filtering' feature help users perform combined filtering of content lists on the front end?

Today, with the rich content of websites, how to help visitors quickly and accurately find the information they are interested in has become a key challenge in content operation.Imagine if your website had thousands of articles, products, or cases, and visitors could only filter through them with simple categorization and search, it would undoubtedly greatly reduce their exploration efficiency and satisfaction.AnQiCMS is well-versed in this field and has provided us with a highly efficient and flexible solution - the "document parameter filtering" feature.This feature is like an intelligent guide, able to meet various conditions we preset

2025-11-08

How to display the Tag list of articles and detailed information of a single Tag as well as associated documents in AnQiCMS?

How to effectively organize and display content in a content management system is the key to improving user experience and search engine friendliness.AnQiCMS provides a powerful Tag feature, helping us to classify and display content more finely.This article will provide a detailed introduction on how to flexibly use tags in AnQiCMS, including displaying tag lists, viewing detailed information of individual tags, and listing documents associated with specific tags.The role of tags in AnQiCMS Tags are a flexible content organization method in AnQiCMS

2025-11-08

How does AnQiCMS display the website message form and customize the form field display?

AnQi CMS: Flexible display and field customization of the website message form In website operation, the message form is an important bridge connecting users and the website.Whether it is to collect customer feedback, provide consulting services, or interact with users, a well-functioning and easy-to-use comment form is particularly important.AnQiCMS (AnQiCMS) is well-versed in this field, providing users with a powerful and flexible feedback form feature, which can be easily displayed on the website front-end and supports in-depth customization of form fields to meet various business needs.### One,

2025-11-08

How to display the friend link list at the bottom of the AnQiCMS website?

In website operation, friendship links play an indispensable role.It is not only an effective way for websites to recommend and share traffic with each other, but also an important means to enhance the weight and visibility of websites in search engines.However, how to conveniently manage and display these links on a website is a practical problem faced by many website managers.AnQiCMS as a system focusing on enterprise-level content management and SEO optimization, provides an intuitive link function and a flexible template calling mechanism, making this process extremely simple.###

2025-11-08