schema - Shopify 架构 - 获取集合 URL
问题描述
我在集合选择器字段的部分中创建了一些架构,如果可能,我正在尝试输出集合的 url。集合选择器已出现在设置中,我选择了一个集合,但我似乎无法输出它的 url,这是代码:
<div class="col-md-6">
<div class="category-box">
**<a href="{{ section.settings.category_top_left.url | default: "#" }}">**
<img src="{{ section.settings.category_top_left_image | img_url: '856x'}}" alt="">
<div class="content">
<h3>{{ section.settings.category_top_left_header }}</h3>
<p>{{ section.settings.category_top_left_text }}</p>
</div></a>
</div>
架构:
{
"type": "collection",
"id": "category_top_left",
"label": "Category Top Left"
}
目前它是输出的默认值,不确定我哪里出错了。
这是我正在查看的文档 - https://shopify.dev/docs/themes/liquid/reference/objects/section#section-settings
提前致谢。
解决方案
如果您查看 Shopify Schema docs for Collection Picker,它指出
集合类型的设置会生成一个下拉列表,该下拉列表会自动填充商店中所有集合的名称。商家从下拉列表中选择的选项的输出是集合的句柄。
由于输出是集合句柄,因此您必须通过句柄获取集合,然后获取图像、标题或所需的任何内容。这样做,你的代码会像
{%- if section.settings.category_top_left -%}
{% assign collectionTopLeft = collections[section.settings.category_top_left] %}
<div class="col-md-6">
<div class="category-box">
<a href="{{ collectionTopLeft.url | default: "#" }}">
<img src="{{collectionTopLeft.image | img_url: '856x'}}" alt="">
<div class="content">
<h3>{{collectionTopLeft.title }}</h3>
<p>{{ collectionTopLeft.description }}</p>
</div>
</a>
</div>
</div>
{%- endif -%}
它的作用是
- 检查设置是否有一些价值
- 通过句柄获取集合
- 使用Collection对象获取不同的属性
推荐阅读
- python - OSError: [WinError 193] %1 不是运行 python 代码的有效 Win32 应用程序
- c# - Directory 类对象是否缓存信息?
- android - Android:在双 SIM 卡设备中以编程方式从特定 SIM 卡发送彩信
- functional-programming - 什么是“强模式”编程语言?
- javascript - 在 React 中使用 setInterval() 将第一项移动到数组的最后一个位置
- python - Pycharm没有显示生成的真实情节
- javascript - 加入通话时出现 CAN_NOT_GET_GATEWAY_SERVER 异常
- javascript - 我需要用香草 javascript 编写的 jQuery slideUp() 中的 setTImeout() 函数吗
- python - 在 Python 中编写 Karatsuba 乘法代码时遇到递归错误
- python-3.x - 如何使用与seaborn相同的kde