How to retrieve the custom fields (such as author, source, additional parameters) of the current article and display them?

In AnQi CMS, managing and displaying custom fields for articles such as authors, sources, or additional parameters is a common and practical need in content operation.The AnQi CMS, with its flexible content model design, provides multiple ways to achieve this goal, helping you easily enrich article information and present it to readers in a personalized way.

Understanding the custom fields of Anqi CMS

Firstly, we need to understand how article custom fields work in AnQi CMS.In Anqi CMS, articles, products, and other content all belong to a specific 'content model'.When you create or edit a content model in the background, you can add a series of custom fields to the model.These fields can include single-line text (such as author, source), multi-line text, numbers, single-choice, multiple-choice, or dropdown selections, and other types.

When defining custom fields, you need to set a 'parameter name' (for display in the background) and a 'call field' (for use in templates, usually in lowercase English letters).This 'call field' has become the key to obtaining and displaying custom fields in our template.

Retrieve the custom fields of the current article and display them

The most direct need for the article being viewed is to retrieve its custom fields. Anqi CMS providesarchiveDetailandarchiveParamsTwo template tags are used to achieve this purpose.

Method one: Directly obtain a specific custom field through the field name.

If you know exactly which custom field you want to display (for example, you have defined a custom field namedauthorandsourceas the name of the custom field), you can use it directlyarchiveDetailLabel, and throughnameAttribute specifies the "calling field" of a custom field.

{# 假设您在后台内容模型中定义了调用字段为“author”和“source”的自定义字段 #}
<div>
    <span>作者:</span>{% archiveDetail with name="author" %}
</div>
<div>
    <span>文章来源:</span>{% archiveDetail with name="source" %}
</div>

In this example,{% archiveDetail with name="author" %}Will directly output the current article content:authorThe value of the custom field. If this field may contain HTML content (such as source information from a rich text editor), you may need to add|safeA filter to ensure that HTML is parsed correctly instead of being escaped:

<div>
    <span>文章内容简介(假设是多行文本自定义字段):</span>{% archiveDetail with name="introduction" %}{{introduction|safe}}
</div>

Method two: Loop through all custom fields

If you want to display all the custom parameters of the article, or if you are not sure about the specific custom fields of the article, thenarchiveParamsThe tag will be a good helper. It can retrieve all the custom fields associated with the current article and allow you to display them one by one through a loop.

{# 获取当前文章的所有自定义参数,并以列表形式展示 #}
<div>
    <h5>更多文章参数:</h5>
    {% archiveParams params with sorted=true %}
        {% for item in params %}
            <div>
                <span>{{item.Name}}:</span> {# item.Name 是自定义字段的“参数名”(后台显示名称) #}
                <span>{{item.Value}}</span> {# item.Value 是自定义字段的实际值 #}
            </div>
        {% endfor %}
    {% endarchiveParams %}
</div>

Here, archiveParams paramsStores all custom fields of the current article inparamsthe variable.with sorted=trueEnsure the parametersparamsIs an ordered array for easy useforLoop traversal. In the loop,item.NameIt will display the custom field name (such as "author") that you set in the background, whileitem.Valueit will display the content filled in for the field.

Custom fields are displayed in the article list

The power of custom fields is not limited to the detail page of a single article, but we can also easily retrieve and display them in article lists (such as related articles, category lists). When you usearchiveListTags to loop through and display article lists, each article item is usually asitema variable exists. At this time, you can accessitem.自定义字段的调用字段to obtain and display its custom fields.

{# 假设在一个文章列表循环中,需要显示每篇文章的作者和缩略图 #}
{% archiveList archives with type="list" categoryId="1" limit="5" %}
    {% for item in archives %}
        <div class="article-item">
            <a href="{{item.Link}}">
                <img src="{{item.Thumb}}" alt="{{item.Title}}" />
                <h3>{{item.Title}}</h3>
            </a>
            <p>作者:{{item.Author}}</p> {# 直接访问item.Author,假设Author是自定义字段的调用字段 #}
            <p>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

In the above code, we directly through{{item.Author}}Obtained and displayed the author information of each article. The premise is that you have defined the calling field in the content model corresponding to the article asAuthorcustom field.

Summary

AnQi CMS through its flexible content model and intuitive template tags, makes it very simple to retrieve and display custom fields.Whether it is to directly refer to a specific field, traverse all fields, or display in an article list, you can choose the most suitable method according to your specific needs to create a more rich and personalized content page.


Frequently Asked Questions (FAQ)

  1. I define a custom field content that contains HTML code, how to display it correctly on the front end?If your custom field content (for example, a rich text editor input long text) may contain HTML tags, and you want these HTML tags to be parsed by the browser on the front end instead of being displayed as plain text, you need to add|safea filter. For example:{{item.MyCustomHtmlField|safe}}.

  2. How to define custom fields for articles in the background?To define the custom fields of an article, you need to log in to the AnQi CMS backend, navigate to the "Content Management" menu under the "Content Model" module.Find the article content model you want to add fields to (for example, "article model"), click edit.In the model editing page, you can click the 'Add Field' button to define new custom fields for the model, including setting parameter names, called fields, field types, etc.

  3. Can I get custom fields for articles not on the current page?Yes.archiveDetailandarchiveParamsAll tags support throughidThe parameter specifies the article ID. For example, if you want to get the author of the article with ID 10, you can use:{% archiveDetail with name="author" id="10" %}. Similarly,archiveParamsYou can alsoidThe parameter to get all the custom fields of the specified article.