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

In the Aiqi CMS, managing and displaying custom fields such as author, source, or additional parameters, which are common and practical needs in content operation.The Anqi CMS, with its flexible content model design, provides various ways to achieve this goal, helping you easily enrich article information and present it to readers in a personalized manner.

Understand the custom fields of Anqi CMS

Firstly, we need to understand how the custom fields of the article work in the security CMS.In the Auto 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 drop-down selections of various 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 lowercase English letters).This 'call field' is the key to obtaining and displaying custom fields in the template.

Get the custom fields of the current article and display them

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

Method one: Directly access the specific custom field by field name.

If you know exactly which custom field to display (for example, you have already defined a custom field namedauthorandsource) you can use it directly.archiveDetailtags, and throughnameProperty specifies the 'Invoked Field' of the 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" %}It will directly output the current article inauthorCustom field value. If this field may contain HTML content (such as source information from a rich text editor), you may need to add|safeFilter to ensure HTML is parsed correctly instead of being escaped:

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

方法二:循环遍历所有自定义字段

If you want to display all custom parameters of the article or you are not sure about the specific custom fields of the article,archiveParamsTags will be your good helper. It can retrieve all associated custom fields of the current article and allows you to display them one by one in 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 paramsThe current article's all custom fields will be stored inparamsthe variable.with sorted=trueparameters to ensureparamsIt is an ordered array, convenient for us to useforLoop through.item.NameIt will display the custom field name you set in the background (such as "Author"),item.Valuewhich will display the content filled in for the field.

Display custom fields in the article list

The strength of custom fields is not limited to the detail page of a single article, but also includes the article list (such as related articles, category lists), where we can easily obtain and display them. When you usearchiveListLabel to loop display article list, each article item usually asitemvariable exists. At this time, you can accessitem.自定义字段的调用字段to get and display its custom field.

{# 假设在一个文章列表循环中,需要显示每篇文章的作者和缩略图 #}
{% 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 access{{item.Author}}to retrieve and display the author information of each article. The prerequisite is that you have defined the call field in the content model corresponding to the article.AuthorCustom fields.

Summary


Common Questions (FAQ)

  1. How to correctly display HTML code in the frontend for the custom field content I defined?If your custom field content (for example, a long text input from a rich text editor) 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|safefilter. For example:{{item.MyCustomHtmlField|safe}}.

  2. How to define custom fields for articles in the background?To define custom fields for the 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 (such as "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, and so on.

  3. Can I get custom fields of articles not on the current page?Yes, it can.archiveDetailandarchiveParamsTags are supported throughidSpecify 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 alsoidparameters to get all custom fields of the specified article.