数字化技能提升教程,数字化时代生存宝典

数字化百科 / 产品文档 / Gallery:如何在缩略图旁显示自定义字段

Gallery:如何在缩略图旁显示自定义字段

查看全部产品文档命名空间:Gallery

Gallery提供的外观主题设定允许显示单个相册或项目的自定义字段区块。如果你想在某相册中缩略图旁显示某项目的自定义字段的话,可以尝试一下下面的外观主题修改方法。

自定义字段模板Callback

如果你只需显示少量字段的话,可以使用customfield.LoadCustomFields callback。此方法的达成需要对外观主题的album.tpl文件做修改。

重要: 如果你在编辑某个Gallery的现有外观主题,请先明确如何编辑已有的外观主题

# 打开外观主题的相册模板文件(如themes/matrix/templates/album.tpl)
# 确认album.tp中项目缩略图,标题和描述的显示位置(查找$child.title)。
# 将下面的内容复制并粘贴到缩略图,标题或描述处
{* 使用customfield.LoadCustomFields callback来获取字段的字串 *}
{g->callback type="customfield.LoadCustomFields" itemId=$child.id}
{if !empty($block.customfield.LoadCustomFields.fields.Item)}
<ul>
{foreach from=$block.customfield.LoadCustomFields.fields item=customField}
<li>{$customField}</li>
{/foreach}
</ul>
{/if}

保存album.tpl之后,应能在相册页面的项目旁看到自定义字段了。Callback的安置和运行都很简单。如果你的网站容量很大并且要考虑性能表现的话,请在你外观主题的theme.inc 文件中载入自定义字段数据。

载入Theme.inc中的自定义字段数据

显示大量项目的繁忙站点通过载入theme.inc 中的自定义字段而不是用callback的话,会得到很好的效果。

# 打开外观主题的theme.inc文件(如themes/matrix/theme.inc)
# 在showAlbumPage()中添加以下内容,就在函数返回行之上,return array (null, 'theme.tpl').
/* 获取自定义字段 */
list ($ret, $children) = GalleryCoreApi::loadEntitiesById($childIds);
if ($ret) {
return array($ret, null);
}

list ($ret, $theme['customFieldValues']) =
CustomFieldHelper::fetchFieldValues($children, 'detail');
if ($ret) {
return array($ret, null);
}

接着,在album.tpl中显示自定义字段

# 打开外观主题的相册模板文件(如themes/matrix/templates/album.tpl)
# 确认album.tp中项目缩略图,标题和描述的显示位置(查找$child.title)。
# 将下面的内容复制并粘贴到缩略图,标题或描述处
{* 显示某child的自定义字段 *}
{if isset($theme.customFieldValues[$child.id])}
<ul>
{foreach from=$theme.customFieldValues[$child.id] key=name item=value}
<li>{$name}: {$value}</li>
{/foreach}
</ul>
{/if}