简体   繁体   English

访问LocomotiveCMS中的单个内容条目

[英]Access single content entry in LocomotiveCMS

I want to show a single content entry (from one content type) on a page and I want to access this page directly with a link. 我想在页面上显示单个内容条目(来自一种内容类型),并且我想直接通过链接访问此页面。

So far I've created the content type "posts" (with wagon generate ...). 到目前为止,我已经创建了内容类型“ posts”(使用wagon generate ...)。 It contains the fields "title","date" and "body". 它包含“标题”,“日期”和“正文”字段。 On the page "posts", all title's of the posts are listed and when you click on one of the title, you should be redirected to the subpage "post" which contains the rest of the content (depending on which post you selected). 在“帖子”页面上,列出了帖子的所有标题,并且当您单击标题之一时,应将您重定向到包含其余内容的子页面“帖子”(取决于您选择的帖子)。

posts.liquid: posts.liquid:

{% extends parent %}
     {% block main %}

            {% for post in contents.posts%}
                   <a href="/{{ post._slug }}"><li>{{ post.date }} - {{ post.titel }}  </li></a>
            {% endfor %}

    {% endblock %}

This lists all posts. 这列出了所有帖子。

post.liquid: post.liquid:

{% extends parent %}
     {% block main %}

       <h2>{{post.title}}</h2>
       <h3>{{post.date}}</h3>
       <p>{{post.body}}</p>

      {% endblock %}

And this shoul be the template for the rest of the content on a single page. 这应该是单个页面上其余内容的模板。

How can I link the list elemnts to the correct post? 如何将列表元素链接到正确的帖子? I'm using wagon to develop the site local. 我正在用旅行车在本地开发网站。

I found a solution to my problem. 我找到了解决问题的方法。

To access only one specific entry in the content type posts, you need to create a template which holds the content with the correct layout. 要仅访问内容类型帖子中的一个特定条目,您需要创建一个模板来保存具有正确布局的内容。

This means you need a file named "content-type-template.liquid" and this file has to be placed in a folder (in my case named "post") to define the parent: 这意味着您需要一个名为“ content-type-template.liquid”的文件,并且必须将该文件放置在一个文件夹(在我的情况下为“ post”)中以定义父级:

/posts.liquid                          # Holds a list of all Posts
/post/content-type-template.liquid     # Holds the layout for only one post

Also in the top of content-type-template.liquid you need to define the content-type and the slug: 同样在content-type-template.liquid的顶部,您需要定义content-type和slug:

--- 
title: Post Template
content_type: post
slug: content_type_template
---

The fields of the content type are now reachable with the following syntax: 现在,可以使用以下语法访问内容类型的字段:

{% extends parent %}
 {% block main %}

   <h2>{{post.title}}</h2>
   <h3>{{post.date}}</h3>
   <p>{{post.body}}</p>

  {% endblock %}

If the content type is for example named "product", you need to rename everything above called "post" with "product". 例如,如果内容类型名为“ product”,则需要将“ post”上方的所有内容重命名为“ product”。

Finally you can reach a single entry with it's slug. 最终,您可以从中获得一个条目。

{% extends parent %}
 {% block main %}

        {% for post in contents.posts%}
               <a href="/post/{{ post._slug }}"><li>{{ post.date }} - {{ post.titel }}  </li></a>
        {% endfor %}

{% endblock %}

Here are some links that helped me: 以下是一些对我有帮助的链接:

http://www.tommyblue.it/2011/02/28/how-to-build-a-website-with-locomotive-cms-from-scratch/ http://www.tommyblue.it/2011/02/28/how-to-build-a-website-with-locomotive-cms-from-scratch/

http://doc.locomotivecms.com/references/api/objects/content-entry http://doc.locomotivecms.com/references/api/objects/content-entry

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

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