How do I reference a specific folder (by name) inside of a specific volume. I cannot use the folder ID, I must use the folder name because the name of the folder is a variable related to specific entries.
1 Answer
You could run a query to fetch the folderId
you need based on the name:
{# Change this to your volumeId #}
{% set volumeId = yourVolumeId %}
{# Change this to your folderName #}
{% set folderName = yourFolderName %}
{# Returns an ID if there is a match, otherwise false #}
{% set folderId = craft
.query()
.select(['id'])
.from('{{%volumefolders}}')
.where(['=','parentId', volumeId])
.andWhere(['=','name', folderName])
.scalar() %}
{% if folderId %}
{% set assets = craft.assets().folderId(folderId).all() %}
{% for asset in assets %}
...
{% endfor %}
{% endif %}