简体   繁体   English

Locomotive CMS:通过 API 访问 content_type 条目

[英]Locomotive CMS: Access content_type Entries via API

How can I access content_type entries via Locomotive CMS RESTful API?如何通过 Locomotive CMS RESTful API 访问 content_type 条目? I can find only limited documentation .我只能找到有限的文档

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'

But now I want to access my content_entries and display their data on a page.但现在我想访问我的 content_entries 并在页面上显示它们的数据。

Background背景

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.我正在 Locomotive CMS 中构建自定义导航片段,并且我希望 content_type 条目与页面一起列在导航中。

Specifically (and obviously) I only want entries listed if the content_type has a content_type_template set up.具体(并且显然)我只希望在 content_type 设置了 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:我最初认为我可以简单地遍历我的页面,使用 slug content_type_template定位任何页面,获取该页面的句柄并将其设置为变量,然后使用该变量获取如下条目:

{% 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 %}

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.根据我在这里的StackOverflow 问答,似乎不可能以这种方式使用 Liquid 变量。

Hence the need to access content_type entries via some other method, eg the Locomotive CMS REST API.因此需要通过一些其他方法访问 content_type 条目,例如 Locomotive CMS REST API。

Access content_type entries via the Locomotive CMS RESTful API as follows:通过 Locomotive CMS RESTful API 访问 content_type 条目,如下所示:

{% 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 %}

[content_type slug] can be replaced with a Liquid varible without any issues. [content_type slug]可以用 Liquid 变量替换,没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM