jekyll blog title to include pagination variables - jekyll

I am trying to get Page 2+ of my blog to have a different title for search engines to index.
I have read several other stackoverflow answers stating that you cannot use liquid tags in the front matter yaml. One suggested to use JS to update the title, however this will not work for me as I want the search engine to index the parsed title.
I thought there may be another way. I can perhaps create a HTML page for each of my pages. I would like to do that without having to manually add each one of my posts into each of the pages (resulting in an ongoing time consuming task each time I post a new article).
I was thinking I could make one page for 1-10, another page for 11-20, etc... Something like this:
---
title: Blog Page 2
---
{% for post in paginator.posts %}
{% if post.index > 10 %}{% if post.index <= 20 %}
<div class="post-preview">
<a href="{{ post.url | prepend: site.baseurl }}">
<h2 class="post-title"> {{ post.title }}</h2>
{{ post.excerpt }}
</a>
</div>
{% endif %}{% endif %}
{% endfor %}
It seems there is no post.index variable available. Is there anything similar I can use?
Or are there any other ways to achieve "Blog Page X" in my title?

Supposing that your head tags are in your _includes/head.html file. In the title tag just add :
{% if paginator %} - page {{ paginator.page }}{% endif %}
Your title tag now looks like this :
<title>
{% if page.title %}{{ page.title }}{% else %}{{ site.title }}{% endif %}
{%if paginator %} - page {{ paginator.page }}{% endif %}
</title>

Related

How to list all post categories in Jekyll?

I was thinking of using the following code snippet but it does not return anything...
{% for category in site.categories %}
<li><strong>{{ category.title }}</strong></li>
{% endfor %}
I'm also using Jekyll's original category page generator plugin, but I cannot figure out how to list all the post categories (for a blog page sidebar) that are being used?
Try this:
{% for category in site.categories %}
<li><strong>{{category|first}}</strong></li>
{% endfor %}
Not sure if something has changed, but something bad happened when I used Leon's answer (seemed like the loop never ended and I got an OOM). Here is what did work for me:
{% for category in site.categories %}
{% capture category_name %}{{ category | first }}{% endcapture %}
{{category_name}}
{% endfor %}
Source is the ever-helpful https://blog.webjeda.com/jekyll-categories/

Get page variable from post variable in Jekyll?

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 %}
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.
If you have a special_var variable defined in a post front matter you can get it like this :
{% for post in site.posts %}
<p>This is my var {{ post.special_var }}.</p>
{% endfor %}
See Jekyll documentation here.

How do I display most recent blog post on Octorpess/Jekyll landing page

I created a landing page for my octopress website and I'd like to have the most recent blog post displayed on the home page, but just the most recent blog. I am not sure quite how to proceed. Is there a code like {% include post %} that will allow me to do this?
Thanks.
As per usual, I tend to find the solution after I ask it.
On home page:
<div class="blog-index">
{% assign post = site.posts.first %}
{% assign content = post.content %}
{% include custom/asides/recent_post.html %}
</div>
In separate document saved to custom/asides/recent_post.html:
<h2 class="entry-title">
{% if post.title %}
{{ post.title }}
{% endif %}
</h2>
<div class="entry-content">{{ content | excerpt }}</div>
<a class="btn btn-default" href="{{ root_url }}{{ post.url }}">{{ site.excerpt_link }}</a>
Found solution here: https://gist.github.com/nimbupani/1421828

How to show first post from category in Jekyll with Liquid

I can't find a solution. I have three categories: tuts, news, code.
The newest post is categorized in tuts. But I want to show the last and newest post in news. I tried the following, but obviously it doesn't show anything, because if I limit the loop to the first item, which is the tuts item, the loop stops.
{% for post in site.posts limit:1 %}
{% if post.categories contains 'news' %}
NEWS</strong> › {{ post.title }}
{% endif %}
{% endfor %}
How do I show the first posting from a special category? Can I loop directly through a chosen category like this? If yes, what is the correct syntax?
{% for post in site.posts.categories.news limit:1 %}
NEWS</strong> › {{ post.title }}
{% endfor %}
Yes, it's possible to directly loop all posts for a certain category or tag.
It's just:
{% for post in site.categories['news'] limit:1 %}
{{ post.title }}
{% endfor %}
It's the same for tags, you just have to replace site.categories['news'] by site.tags['news'].

Jekyll code in jekyll

I'm creating a bird's eye view tutorial for Jekyll, to be hosted on Github pages (on my blog that runs on Jekyll). So, I want to put some code there. If I put the following:
{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
<h2>
{{ post.title }}
</h2>
{% endif %}
{% endfor %}
(all lines after tabspaces), it doesn't render as code, rather, it executes. How do I stop it from executing and render it as code?
The {%...%} syntax used by Jekyll is part of the Liquid templating engine. To escape these tags, and so show them literally, you should use the raw tag.
You will probably want to combine this with the markdown syntax for code blocks. With Redcarpet you can use the triple backtick syntax. It doesn’t matter if you put the backticks inside the raw tags or the other way round:
{%raw%}
```
{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
<h2>
{{ post.title }}
</h2>
{% endif %}
{% endfor %}
```
{%endraw%}
Enclose your code in backticks:
(tested with redcarpet markdown engine)
```
{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
<h2>
{{ post.title }}
</h2>
{% endif %}
{% endfor %}
```
There are at least three options exist, taht you can use to format code in Jekyll:
highlight - Jekyll has built in
{% highlight java %}
ValidationResult validationResult = NetLicensing.LicenseeService.validate(context, licenseeNumber);
{% endhighlight %}
example: https://raw.githubusercontent.com/Labs64/netlicensing.io/gh-pages/_drafts/2010-09-16-post-template.md (see Syntax highlighting section)
backtick - GitHub style
```java
ValidationResult validationResult = NetLicensing.LicenseeService.validate(context, licenseeNumber);
```
HTML pre/code - HTML can be included in markdown as well
<pre><code>
ValidationResult validationResult = NetLicensing.LicenseeService.validate(context, licenseeNumber);
<code/></pre>