Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get page variable from post variable in Jekyll?

Tags:

jekyll

liquid

I'm iterating over all the posts in my site like so

{% for post in site.posts %}
  // code
{% endfor %}

I want to access some variable that I have stored at the page level. How can access it? I wasn't able to find anything after googling for awhile. I want to do something like

{% for post in site.posts %}
  post.page.special_var
{% endfor %}
like image 491
tlnagy Avatar asked Oct 18 '22 19:10

tlnagy


1 Answers

Jekyll support both post and page, so it is depend on you, which type of variable you want to access.

For example here is your post front matter.

---
layout:post
title: jekyll test
categories: jekyll
---

So in head.html, I am using this.

<title>{% if page.title %}{{ page.title }}{% endif %}</title>

I am using page to access that variable because there are too many pages like about or contact or privacy policy that does not belongs to jekyll post,so there you can't use post for example post.title to access that variable.

Now, look out these codes

{% for post in site.categories.jekyll reversed limit:10 %}
<span><a href="{{ post.url }}">{{ post.title}}<a/></span>
{% endfor %}

Here you note that, I am using loop, because I want to access same variable from multiple post, and that variable was jekyll .

Here I am using post.title but you can even use page.title, because it is globally accessible.

For fun :

I am using reversed, so you can order your post in which date you are created, older post will show at first.

I am using limit:10 because, I want to show only 10 post per page.

like image 95
Goyllo Avatar answered Jan 04 '23 06:01

Goyllo