机车CMS:通过API访问content_type条目 [英] Locomotive CMS: Access content_type Entries via API
问题描述
如何通过机车CMS RESTful API访问content_type条目?我只能找到我设法使用以下方法获取身份验证令牌:
I've managed to get my auth token using the following:
curl -v -H 'Content-Type: Application/json' -d '{"email":"your@email.abc", "password":"foo"}' 'https://your.locomotive.instance.tld/locomotive/api/v3/tokens.json'
但是现在我想访问我的content_entries并将其数据显示在页面上.
But now I want to access my content_entries and display their data on a page.
我正在Locomotive CMS中构建自定义导航片段,并且希望content_type条目与页面一起在导航中列出.
I'm building a custom navigation snippet in Locomotive CMS and I want content_type entries to be listed in the navigation along with pages.
特别(显然),我只希望列出content_type设置了content_type_template的条目.
Specifically (and obviously) I only want entries listed if the content_type has a content_type_template set up.
我最初以为我可以简单地遍历页面,找到带有 content_type_template
的代码,抓住该页面的句柄并将其设置为变量,然后使用该变量获取像这样的条目:
I initially thought that I could simply loop through through my pages, locate any with the slug content_type_template
, grab the handle of that page and set it as a variable and then use the variable to get the entries like so:
{% for loopPage in page.children %}
{% assign myContentType = '' %}
{% if page.slug == 'content_type_template' %}
{% assign myContentType = loopPage.handle %}
{% for item in contents.myContentType %}
<li>a href="{{ item._slug }}">{{ item.name }}</a></li>
{% endfor %}
{% else %}
<li><a href="{% path_to loopPage %}">{{ loopPage.title }}</a></li>
{% endif %}
{% endfor %}
但这不起作用.根据我的StackOverflow Q&此处,看来不可能以这种方式使用液体无花果.
But this doesn't work. As per my StackOverflow Q & A here, it seems it isn't possible to use a Liquid varibale in this way.
因此需要通过其他方法(例如机车CMS REST API.
Hence the need to access content_type entries via some other method, e.g. the Locomotive CMS REST API.
推荐答案
通过机车CMS RESTful API访问content_type条目,如下所示:
Access content_type entries via the Locomotive CMS RESTful API as follows:
{% action "get content_type entries" %}
var contentData = false;
contentData = callAPI('GET', 'https://station.locomotive.works/locomotive/api/v3/content_types/[content_type slug]/entries.json', {
data: {
},
headers: {
'X-Locomotive-Account-Email': '[my-account-email-adddress]',
'X-Locomotive-Site-Handle': '[my-site-handle]',
'X-Locomotive-Account-Token': '[my-account-token]'
}
});
setProp('contentData', contentData);
{% endaction %}
可以用Liquid变量替换
[content_type slug]
,没有任何问题.
[content_type slug]
can be replaced with a Liquid varible without any issues.
这篇关于机车CMS:通过API访问content_type条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!