As an experienced CMS website operation person in the security industry, I know how important the custom field function of the content model is for building rich and personalized website content.It can not only help us organize information flexibly, but also provide users with more accurate filtering and display experience.I will elaborate in detail on how to obtain the custom field parameters of the content model definition in Anqi CMS and perform effective filtering and display.
Creating and managing custom field of content model
The foundation of all functions originates from the backend configuration. The flexibility of the content model in AnQi CMS is one of its core advantages.We can create or edit custom content models and add exclusive custom fields according to business needs through the 'Content Management' menu under the 'Content Model' feature.
When adding custom fields, you need to set the "parameter name" (this is the display name of the field in the background), the "call field" (this is the unique identifier used when referencing the field in the template, it must be in English letters, and remember its name), the "field type" (such as single-line text, number, multi-line text, single selection, multiple selection, dropdown selection, etc.), whether it is required, and the default value.Especially for selection type fields, the 'default value' will define the options available.These meticulously configured fields will serve as the basis for your front-end content display and filtering.
How to retrieve custom field parameters of a single document in the template
When we need to display a specific custom field value on a document detail page (for example, a product introduction or an article), Anq CMS provides a concise and intuitive template tag.
You can use it directly.{% archiveDetail with name="您的调用字段名" %}This way to retrieve and output the value of the field. For example, if your product model has a custom field namedproduct_weightits 'call field' is set toweightThen you can display its weight like this in the product detail page template:
<div>产品重量:{% archiveDetail with name="weight" %}</div>
If you want to assign this value to a variable before using it, you can do it like this:
{% archiveDetail productWeight with name="weight" %}{{productWeight}}</div>
This method is suitable when you know exactly which custom field to retrieve and directly display its value.
Loop through the template to retrieve all custom field parameters.
Sometimes, we may want to traverse and display all the filled-in custom fields of a document detail page, such as showing the list of product specifications. At this point,archiveParamsthe tag comes into play.
archiveParamsTags allow you to get all custom fields and their values for the current document or a specified document. You can use it like this:
{% 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 an array object that contains all custom field information. Traverse through it by loopparams,item.NameWill output the "parameter name" you set in the background (displayed in Chinese), anditem.ValueThis will output the specific content of the custom field. This method is very useful for building dynamic and flexible parameter lists.
If you need to perform special display processing for certain fields, or exclude certain fields from display, you can add them in the loop.ifLogic judgment:
{% archiveParams params %}
<div>
{% for item in params %}
{% if item.Name != '内部备注' %} {# 假设您有一个名为“内部备注”的字段不想在前台显示 #}
<div>
<span>{{item.Name}}:</span>
<span>{{item.Value}}</span>
</div>
{% endif %}
{% endfor %}
</div>
{% endarchiveParams %}
Utilize custom fields for content filtering and display
In addition to being displayed on the detail page, the powerful application of custom fields is reflected in the filtering function on the list page, which can greatly improve the efficiency of users searching for content. Anqi CMS provides this forarchiveFiltersandarchiveListLabel combinations are used.
archiveFiltersTags are used to generate the front-end filtering condition interface, such as drop-down boxes, checkbox groups, and so on.It will automatically generate corresponding filter options and links based on the custom fields defined in your content model.
First, use it in your list page template,archiveFiltersto build the filter area:
<div>
<div>内容筛选:</div>
{% archiveFilters filters with moduleId="您的模型ID" allText="全部" %}
{% for item in filters %}
<ul>
<li>{{item.Name}}: </li>
{% for val in item.Items %}
<li class="{% if val.IsCurrent %}active{% endif %}"><a href="{{val.Link}}">{{val.Label}}</a></li>
{% endfor %}
</ul>
{% endfor %}
{% endarchiveFilters %}
</div>
Here, moduleIdSpecified the content model you want to filter (for example, the article model ID is usually 1, and the product model ID is usually 2).allTextThe parameter defines the display text for the 'All' option.filtersAn object contains a series of filterable fields (eachitem), each field contains all its optional filter values (eachval)val.Linkwill generate a URL with the corresponding filter parameters,val.LabelIs the display text of the filter option,val.IsCurrentThen determine whether the current option is selected, convenient for you to add highlight style.
When the user clicks on a filter option, the page URL will include the corresponding query parameters, for example?color=红色&size=L.
Next,archiveListThe label will automatically receive the query parameters from these URLs and use them to filter and display the documents that meet the conditions. InarchiveFiltersthe area below, you can use it as usual.archiveListDisplay the document list:
<div>
{% archiveList archives with type="page" moduleId="您的模型ID" limit="10" %}
{% for item in archives %}
<div>
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
<p>{{item.Description}}</p>
{# 这里可以再次使用archiveParams或archiveDetail显示自定义字段 #}
{% archiveParams docParams with id=item.Id %}
<ul>
{% for param in docParams %}
<li>{{param.Name}}: {{param.Value}}</li>
{% endfor %}
</ul>
{% endarchiveParams %}
</div>
{% empty %}
<div>暂无符合条件的文档</div>
{% endfor %}
{% endarchiveList %}
</div>
In the abovearchiveListIn, you do not need to manually enter the filtering conditions, the system will automatically identify the custom field parameters in the URL and filter them.By this combination, you can easily implement a dynamic filtering list with complete functions.
Summary
AnQi CMS provides great convenience for our website operation staff with its flexible content model and powerful template tags.From defining personalized content fields in the background, to accurately obtaining and displaying these fields in front-end templates, to building interactive filtering functions, every step aims to help us better manage and present content, thereby improving user experience and website appeal.Master these skills, and you can operate your AnQiCMS site more efficiently, meeting various complex business needs.
Frequently Asked Questions
Q1: I defined a custom field and filled in the content in the content model, but it does not display in the front-end template. Why is that?
Please check that the 'field name called' you use in the template is exactly the same as the one set in the background, including case sensitivity.Next, confirm whether you have saved or updated the document, the content of the newly added or modified fields needs to be saved before it takes effect.If the field type is image, file, etc., also make sure the path is correct.Finally, check if the website cache has been cleared, sometimes the Anq CMS has cache, refreshing the cache can solve most display problems.
Q2: Can I use multiple custom fields as filter conditions at the same time? For example, filter products that are both 'red' and 'XL size'?
Absolutely, you can. When users go through the front endarchiveFiltersWhen selecting multiple filter conditions, each selected condition is attached to the URL as an independent query parameter (for example/products?color=红色&size=XL)archiveListThe label will automatically parse and apply all these query parameters to achieve combined filtering of multiple conditions. You just need to configure these fields as filterable in the background content model.
Q3: Can custom fields only be used in document models, or do categories and single pages also have similar functions?
The flexibility of AnQi CMS is reflected in its design philosophy. Yes, categories and single pages also support custom fields. For example, you can use{% categoryDetail with name="您的自定义字段" %}to get the custom fields of the category, or{% pageDetail with name="您的自定义字段" %}Get custom fields for a single page. This allows you to add rich and personalized information for different types of content.