As a senior security CMS website operations personnel, I am well aware of the importance of the custom field function of the content model in building rich and personalized website content.It not only helps us organize information flexibly, but also provides users with more accurate filtering and display experience.Next, I will elaborate on how to obtain the custom field parameters of the content model definition in Anqi CMS and perform effective filtering and display.

Creation and management of custom fields in the content model

Firstly, the foundation of all functions originates from the configuration on the backend.In AnQi CMS, the flexibility of the content model is one of its core advantages.We can create or edit custom content models according to business needs under the 'Content Management' menu's 'Content Model' feature, and add exclusive custom fields to these models.

When adding custom fields, you need to set the 'parameter name' (this is the display name of the field in the background), 'field name' (this is the unique identifier used when referencing the field in the template, and it must be in English letters, and remember its name), 'field type' (such as single-line text, number, multi-line text, single selection, multiple selection, dropdown selection, etc.), 'whether required', and 'default value'.Especially for choice type fields, the "default value" defines the options available for selection.These meticulously configured fields will serve as the foundation for your front-end content display and filtering.

Retrieve custom field parameters for a single document in the template

When we need to display a specific custom field value on a document detail page (such as a product introduction or an article), Anqi CMS provides a concise and intuitive template tag.

You can directly use{% archiveDetail with name="您的调用字段名" %}this method to retrieve and output the value of the field. For example, if your product model has a custom field namedproduct_weightand its 'Field Call' is set toweightThen, in the product detail page template, you can display its weight like this:

<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 for situations where you know exactly which custom field you want to retrieve and directly display its value.

Loop through the template to retrieve all custom field parameters

Sometimes, we might want to iterate and display all filled custom fields in a document detail page, for example, showing the list of a product's 'specifications'. At this time, archiveParamsTags come into play.

archiveParamsTags allow you to retrieve 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 containing all custom field information. By iterating overparams,item.Nameit will output the "parameter name" you set in the background (displayed in Chinese)item.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 it inside the loop.ifLogical 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 %}

Use 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 providesarchiveFiltersandarchiveListThe combination of tags usage.

archiveFiltersTags are used to generate the front-end filtering condition interface, such as dropdown boxes, checkbox groups, etc.It will automatically generate corresponding filter options and their links based on the custom fields that are allowed to be filtered as defined in your content model.

Firstly, 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.filtersThe object includes a series of filterable fields (each)item), each field contains all its optional filter values (each)val).val.LinkIt will generate a URL with the corresponding filter parameters,val.LabelIs the display text of the filter option,val.IsCurrentthen judge whether the current option is selected, convenient for you to add highlight style.

When the user clicks on a filter option, the corresponding query parameter will be added to the page URL, for example,?color=红色&size=L.

Next,archiveListLabels will automatically receive the query parameters from these URLs and filter out the documents that meet the conditions for display.archiveFiltersBelow the area, you can use it as usual.archiveListShow 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>

the abovearchiveListYou do not need to manually enter the filtering conditions, the system will automatically identify the custom field parameters in the URL and perform the filtering.Through this combination, you can easily implement a feature-rich dynamic filtering list.

Summary

The Anqi CMS provides great convenience for our website operators with its flexible content model and powerful template tags.From defining personalized content fields in the background, to accurately retrieving and displaying these fields in frontend templates, to building interactive filtering functions, every step aims to help us better manage and present content, thereby enhancing user experience and the appeal of the website.Master these skills and you will be able to operate your AnQiCMS site more efficiently to meet various complex business needs.


Frequently Asked Questions

Q1: I have defined custom fields and filled in content in the content model, but I cannot display it in the front-end template. Why is that?

Please check if the 'Field Name Used in Template' you are using is exactly consistent with the one set in the backend, including the case sensitivity.Secondly, confirm whether you have saved or updated the document, the content of the fields added or modified needs to be saved before it takes effect.If the field type is image, file, etc., also ensure that the path is correct.Finally, check if the website cache has been cleared, as Secure CMS sometimes has cache, and refreshing the cache can solve most display issues.

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'?

Yes, you can absolutely do that. When a user navigates through the frontend by archiveFiltersWhen selecting filter conditions generated, multiple selected filter conditions will be attached to the URL as independent query parameters (for example,/products?color=红色&size=XL).archiveListThe tags 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 backend content model.

Q3: Can custom fields only be used in document models, or do categories or single pages also have similar features?

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="您的自定义字段" %}To retrieve custom fields for a single page. This allows you to add rich and personalized information for different types of content.