简体   繁体   中英

jekyll post loop not working on home page. Showing code as it is

I want to show latest posts in my bolg.html page. So i used Liquid syntax

But when i used the liquid shown below and run locally, the page shows the code as it is and it doesn't fetch posts from my _posts folder.

The liquid syntax is given below:

liquid syntax

and then the out put is as given below

output

Please help me. Also i'am using a template called Triangle from bootstrap. and i'am hosting on github.

I think I know what's wrong. Your loop is not working because you said {% for blogpost in site.posts%} and then you call the loop by post and not blogpost . Try changing the for loop to:

{% for post in site.posts %} 

and you should be fine!

If this answer is correct or useful, please mark it! Thanks! ;)

This might be a bit late but I just had a similar issue.

I originally had this in my default.html

---
page.title: My Blog
---
<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>{{ page.title }} | My Blog - Musings of a Software Developer from Norwich</title>
    <link rel="stylesheet" href="{{'/assets/main.css' | prepend: site.baseurl}}">
</head>   
<body>
    {% include nav.html %}

    <div class="content">
        {{ page.content }}
    </div>
    <script src="{{'/node_modules/jquery/dist/jquery.min.js' | prepend: site.baseurl}}"></script>
    <script src="{{'/node_modules/popper.js/dist/popper.min.js' | prepend: site.baseurl}}"></script>
    <script src="{{'/node_modules/bootstrap/dist/js/bootstrap.min.js' | prepend: site.baseurl}}"></script>
</body>

</html>

and in my blog.html, I had

---
title: Blog
layout: default
---

<h1>Latest Posts</h1>

<ul>
    {% for post in site.posts %}
    <li>
        <a href="{{ post.url }}">{{ post.title }}</a>
    </li>
    {% endfor %}
</ul>

Answer

I noticed that when I removed the layout from the frontmatter, then the loop was running correctly, so I put my layout back in and it stopped working. Then on closer comparison with another site I was doing, I noticed in my layout I had {{ page.content }} . So I updated this to just be {{ content }} and hey presto, I got my theme and it looping over the blog posts like I wanted it too. Hopefully this might help someone in the future

I'm really late to the party but better late than never eh?

By looking at the screenshots you've posted it seems you're using {{ page.content }} in your layout template as liquid tags don't seem to be rendering as you expect them to. Maybe you need to use {{ content }} instead so your content renders correctly before being outputted to the page?

I solved this by deleting the index.html in the root directory and creating an index.md. After this, everything worked as expected!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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