In the daily operation of AnQiCMS, we often need to add some personalized information outside of the standard fields for different types of content (such as articles, products), such as specific authors for articles, detailed prices for products, and production dates, etc.AnQiCMS is a powerful content model feature that allows us to flexibly define these custom fields, and conveniently call them and display them in templates, thus meeting various content display needs.
To achieve this goal, we need to go through two main steps: first, define these custom additional parameters in the AnQiCMS backend, and then correctly call and display them in the website front-end template.
Define the custom additional parameters of the document
AnQiCMS provides a content model for customizing the structure of different types of content.We can add exclusive fields for articles, products, or other custom content types through the content model.
First, you need to log in to the AnQiCMS admin interface, navigate to the "Content Management" section, and then select the "Content Model".Here you can see the built-in 'article model' and 'product model', and you can also create new custom models.Select the model you want to add additional parameters, click edit to enter the details page.
In the editing interface of the content model, you will find an area for 'custom fields of the content model'.This is the core of defining additional parameters.
- Parameter NameThese are the field names displayed on the back-end management interface, such as 'Article Author', 'Product Price', or 'Production Date'.
- Field invocationThis is the unique identifier used when calling this parameter in the template, it is usually recommended to use lowercase English letters, for example
author/priceorproductionDate. This field name will be directly mapped to the database, so it is necessary to ensure its uniqueness and standardization. - Field type: AnQiCMS supports various field types, such as single-line text (suitable for author names, simple prices), numbers (suitable for precise prices), multi-line text, single choice, multiple choice, and dropdown selection, etc.Selecting the appropriate field for your data type can ensure accuracy.
- Mandatory?: Determine whether this field is required based on business requirements.
- Default valueYou can set a default value for the field. For selection type fields, this is used to enter all possible values, one per line.
For example, if you want to add a 'article author' field to the article model, you can set the 'parameter name' to 'article author', and the 'field name' to
author,“field type” is “single-line text”. If you want to add a “product price” to the product model, you can set the “parameter name” to “product price”, and the “called field” ispriceThe field type is 'number' or 'single-line text' (if the price contains a currency symbol).
Save the content model after completing field definition.
Enter data for custom additional parameters.
When a custom field is defined in the content model, these custom fields will appear on the document editing page when you add or edit documents belonging to the model.
When creating or editing a document in the background, you will see a collapsible area named "Other parameters".Expand this area, and you will find all the custom fields you have defined in the content model.authorField, so when editing articles, a 'Article Author' input box will appear here, allowing you to enter the specific author name. Similarly, under the product model,priceThe field will also be displayed in the corresponding manner.
You only need to fill in the corresponding data according to the actual situation of the document in these custom fields. This data will be called and displayed in the front-end template.
Call custom additional parameters in AnQiCMS template
AnQiCMS provides two main ways to call custom additional parameters in the template: usingarchiveDetailLabel directly retrieves the value of a single field or usesarchiveParamsLabel traverses all custom fields.
UsearchiveDetailLabel retrieves a single custom field.
When you know exactly which custom field to call and only need to retrieve the value of that field,archiveDetailThe tag is the most direct choice. This tag is usually used on the document detail page, and it will default to getting the parameters of the current document.
The basic usage is{% archiveDetail with name="调用字段名" %}.
For example, if you define the 'call field' of the article author asauthorThen in the template, you can call and display the article author like this:
<div>文章作者:{% archiveDetail with name="author" %}</div>
If you define the 'call field' of the product aspriceThen you can display the product price like this:
<div>产品价格:{% archiveDetail with name="price" %}</div>
You can also assign the obtained value to a variable to make it more flexible in the template:
{% archiveDetail articleAuthor with name="author" %}
<div>作者:{{ articleAuthor }}</div>
{% archiveDetail productPrice with name="price" %}
<div>价格:{{ productPrice }}</div>
archiveDetailThe tag also supports passing throughidortokenThe parameter specifies the fields to retrieve for a specific document, but usually in the document detail page, it will automatically identify the current document.
UsearchiveParamsThe tag traverses all custom fields.
When you need to display all the custom fields of a document, or need to traverse these fields in a structured way,archiveParamsThe tag will be more applicable. This tag will return all custom fields of the document as an array object, and you can iterate over it.
The basic usage is{% archiveParams 变量名称 %}Then useforLoop to iterate over this variable.
For example, to display all custom parameters of a document:
{% archiveParams params %}
<div>
{% for item in params %}
<div>
<span>{{ item.Name }}:</span> {# 参数名,如“文章作者” #}
<span>{{ item.Value }}</span> {# 参数值,如“张三” #}
</div>
{% endfor %}
</div>
{% endarchiveParams %}
In the above code,paramsIs the variable name you have customized, it will contain an array containing all custom fields. EachitemIs available in the loopName(i.e., the parameter name you set in the background) andValue(i.e. the parameter value you enter in the document) two properties.
archiveParamsThe label has an optionalsortedparameter. Whensorted=true(default value) it returns a fixed sorted array object, suitable forforloop; whensorted=falseIt returns an unordered map object, you can directly access it through the key (i.e., the 'calling field'):
{# 使用无序的map对象访问 #}
{% archiveParams params with sorted=false %}
<div>
<div>作者:{{ params.author.Value }}</div>
<div>价格:{{ params.price.Value }}</div>
</div>
{% endarchiveParams %}
This method is very convenient when you need to access specific custom fields directly instead of iterating over all fields.
Calling custom fields in the document list loop
If you are usingarchiveListTags can also be looped to display multiple documents, and you can also display custom fields of each document byarchiveListusing the loop internallyarchiveParamslabel and pass the current document'sId.
{% archiveList archives with type="page" limit="10" %}
{% for archiveItem in archives %}
<div>
<h3><a href="{{ archiveItem.Link }}">{{ archiveItem.Title }}</a></h3>
{# 在这里调用当前文档的自定义参数 #}
{% archiveParams currentArchiveParams with id=archiveItem.Id %}
<div>
{% for param in currentArchiveParams %}
<span>{{ param.Name }}:{{ param.Value }}</span>
{% endfor %}
</div>
{% endarchiveParams %}
</div>
{% endfor %}
{% endarchiveList %}
By using the above method, you can flexibly define, fill in, and call the custom document extra parameters in the AnQiCMS template, thus achieving richer and more personalized content display.
Frequently Asked Questions
Why can't I call the custom field I set in the template?
First, make sure the name of the 'call field' you defined in the content model matches the one in the templatename
How to display custom fields in a document list?
To display custom fields in the document list (viaarchiveListthe list obtained by the tag) for each document, you need toarchiveListoffornested inside the loop usingarchiveParamsLabel. When callingarchiveParamsbe sure to pass throughidthe parameters will pass the current looped document'sIdin, for example{% archiveParams params with id=item.Id %}of whichitemIsarchiveListthe current document variable in the loop.
I can directly be inarchiveDetailCan I call a custom field in the tag?
Yes, you can directly be inarchiveDetailThrough the tagnameSpecify the name of the calling field for your custom field to get its value. For example, if you have a custom field namedproductWeightyou can use it directly.{% archiveDetail with name="productWeight" %}To get and display it. This is the simplest way to get a single specific custom field.