Search code examples
htmlmarkdownblogs

How to show the correct title for my blog post and not just the latest title using markdown


I am trying to creating a blog on my website using markdown but I am having trouble with the blog titles. I have created a loop to bring in the latest title and excerpt onto my home page which works fine.: {% for post in site.posts limit: 1 %}

On my site I have a list of my blogs (https://jenjnif.github.io/blog.html) and when you click on one to read the whole blog I would the title for only that blog to show up.

I used the same code as above {% for post in site.posts limit: 1 %} to only show one title as all blog titles were showing without this but obviously then it will only show the latest title regardless of which blog I am actually viewing. Here is the markdown for that page:

---

layout: default

---

<div id="post">
<div class="blog-header">
			{% for post in site.posts limit: 1 %}
</div>
<h2 class="blog-title">{{ post.title }}</h2>
{% endfor %}
{{ content }}
</div>

I know the limit: 1 will not work because it is only then going to take the latest title but I don't know how else to get only one title showing up on each blog post. Is there a way to make sure only one title shows - only showing the correct, current blog title, not all of them? All files can be found in my GitHub repository: https://github.com/jenjnif/jenjnif.github.io


Solution

  • When using Jekyll and its Liquid templating engine, they have several sets of variables. Instead of using a for loop to go through all the posts, you can use page.title to get the title for the current blog post.

    <div id="post">
    <div class="blog-header">
    </div>
    <h2 class="blog-title">{{ page.title }}</h2>
    {{ content }}
    </div>