delaneymethod/craft-category-groups
最新稳定版本:1.0.0
Composer 安装命令:
composer require delaneymethod/craft-category-groups
包简介
Two field types for Craft CMS that lets you select one or more Category Groups or one or more Categories from one or more Category Groups.
关键字:
README 文档
README
Two field types for Craft CMS that:
- lets you select one or more Category Groups.
- lets you select one or more Categories from one or more Category Groups.
Install
- Add the repo to your project and install via Composer:
cd /path/to/project composer require delaneymethod/category-groups && php craft plugin/install category-groups
Usage
CategoryGroupField
- In the dropdown/radio buttons mode your field normalizes to a
CategoryGroup|null. - In checkbox mode you return an
array<CategoryGroup>.
Print the selected group name(s):
{% set fieldValue = entry.myCategoryGroupsFieldHandle ?? null %}
{% if fieldValue %}
{% if fieldValue is instance of('craft\\models\\CategoryGroup') %}
<p>Selected category group: {{ fieldValue.name }}</p>
{% elseif fieldValue is iterable %}
<p>Selected category groups:</p>
<ul>
{% for categoryGroup in fieldValue %}
<li>{{ categoryGroup.name }}</li>
{% endfor %}
</ul>
{% elseif fieldValue is string %}
{% set categoryGroup = craft.app.categories.getGroupByUid(fieldValue) %}
{% if categoryGroup %}
<p>Selected category group: {{ categoryGroup.name }}</p>
{% endif %}
{% else %}
<p>No category group selected.</p>
{% endif %}
{% else %}
<p>No category groups selected.</p>
{% endif %}
Use the selected category group to list its categories (current site)
{% if fieldValue is instance of('craft\\models\\CategoryGroup') %}
{% set categories = craft.categories().groupId(fieldValue.id).siteId(craft.app.sites.currentSite.id).status(null).orderBy('title asc').all() %}
{% if categories %}
<h4>Category group: {{ fieldValue.name }}</h4>
<ul>
{% for category in categories %}
<li><a href="{{ category.url }}">{{ category.title }}</a></li>
{% endfor %}
</ul>
{% else %}
<p><em>No categories found in {{ fieldValue.name }}.</em></p>
{% endif %}
{% elseif fieldValue is iterable %}
{% for categoryGroup in fieldValue %}
{% set categories = craft.categories().groupId(categoryGroup.id).siteId(craft.app.sites.currentSite.id).status(null).orderBy('title asc').all() %}
{% if categories %}
<h4>Category group: {{ categoryGroup.uid }}</h4>
<ul>
{% for category in categories %}
<li><a href="{{ category.url }}">{{ category.title }}</a></li>
{% endfor %}
</ul>
{% else %}
<p><em>No categories found in {{ categoryGroup.name }}.</em></p>
{% endif %}
{% endfor %}
{% elseif fieldValue is string %}
{% set categoryGroup = craft.app.categories.getGroupByUid(fieldValue) %}
{% if categoryGroup %}
{% set categories = craft.categories().groupId(categoryGroup.id).siteId(craft.app.sites.currentSite.id).status(null).orderBy('title asc').all() %}
{% if categories %}
<h4>Category group: {{ categoryGroup.name }}</h4>
<ul>
{% for category in categories %}
<li><a href="{{ category.url }}">{{ category.title }}</a></li>
{% endfor %}
</ul>
{% else %}
<p><em>No categories found for {{ categoryGroup.name }}.</em></p>
{% endif %}
{% else %}
<p><em>No category group found for {{ fieldValue }}.</em></p>
{% endif %}
{% endif %}
CategoryGroupsField
- Normalizes to a
CategoryQuery. - You can iterate, count, limit, etc.
List selected categories (title + group)
{% set categoryQuery = entry.myCategoryMultipleGroupsFieldHandle ?? null %}
{% if categoryQuery %}
{% set categories = categoryQuery.status(null).all() %}
{% if categories %}
<ul>
{% for category in categories %}
<li><a href="{{ category.url }}">{{ category.title }}</a> ({{ category.group.name }})</li>
{% endfor %}
</ul>
{% else %}
<p>No categories selected.</p>
{% endif %}
{% else %}
<p>No categories selected.</p>
{% endif %}
Count / limit / paginate
{% set categoryQuery = entry.myCategoryMultipleGroupsFieldHandle ?? null %}
{% if categoryQuery %}
{% set count = categoryQuery.count() }}
{% if count > 1 %}
<p>{{ count }} categories selected.</p>
{% else %}
<p>1 category selected.</p>
{% endif %}
{% set firstFiveCategories = categoryQuery.status(null).limit(5).all() %}
{% if firstFiveCategories %}
<p>First five categories:</p>
<ul>
{% for category in firstFiveCategories %}
<li><a href="{{ category.url }}">{{ category.title }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-30