I am using Jekyll and I want to have a link to my homepage in all the pages of my site except, of course, in my home page. I thought to put it in the layout.html with a conditional that is true when the actual page is not the home page. Something like this:
{% if site.baseurl != page.url %}
Home
{% endif %}
But it is not working. Which is the right way to do this?
Thanks!
As you home page url is supposed to always be "/".
You can do :
{% if page.url != "/" %}
Home
{% endif %}
Related
The official Jekyll tutorial has an entire section on using a YAML file to define a custom sequence of pages: https://jekyllrb.com/tutorials/navigation/
But it doesn't mention anywhere how one might create previous/next navigation buttons on the pages within that sequence, which is particularly ironic considering that the tutorial itself has them.
I've come up with some Liquid to determine the index of the current page:
{% for item in site.data.encore.docs %}
{% if item.url != page.url %}
{{ item.title }}: {{ forloop.index }}
{% else %}
<strong> This page index: {{ forloop.index }}</strong>
{% assign this_page_index = forloop.index %}
{% break %}
{% endif %}
{% endfor %}
but getting the index of the previous page via {% decrement this_page_index %} always returns -1 for some reason, and something like {% assign previous = this_page_index - 1 %} isn't valid Liquid. Same goes for trying to get the next page with similar methods.
What's the ideal way to accomplish this? I've searched every way I can think of and not found anything.
You can find the code for Jekyll's own navigation on their tutorials page by sifting through their GitHub repo until you get to their section_nav_tutorials.html, but it appears the way to do it is very close to what you have.
Liquid doesn't respect you doing math directly, you have to use a filter. For you, you'd use {% assign previous = this_page_index | minus: 1 %}.
I have an _includes folder, and within it I have nav subfolder containing a nav.html file.
I've been trying to check if nav.html exists in nav. If it does, I want to include that nav.html file. If it doesnt exist, there will be a default nav for my template.
Here's the code I have right now:
{% for static_file in site.static_files %}
{% if static_file.path == '/nav.html' %}
{% include nav.html %}
{% endif %}
{% endfor %}
Hope you guys can help me out.
Many thanks in advance
I am not able to find the solution using existing liquid tags so I solved this by using a plugin with a custom liquid tag.
{% capture fileExists %}{% file_exists _includes/nav/custom/nav.html %}{% endcapture %}
{% if fileExists=="true" %}
{% include /nav/custom/nav.html %}
{% else %}
{% include /nav/default/nav.html %}
{% endif %}
See GitHub Repo
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>
As you may know Jekyll uses Liquid tags, and the Liquid templating engine has support for If / Else / Unless statements. Does anyone know how to show a specific content only on the 'homepage` (& NOT paged)?
Got it!
UPDATE: This doesn't work, if you don't yet have enough posts for pagination. That is, it only works if you have at least page1 and page2.
As you may already know, Jekyll supports pagination. So, to target just the Index/Home page (and specifically only the first page, i.e. page1 and NOT page2, page3 ...), you can use this:
{% if paginator.next_page == 2 %}
<div id="welcome">Hello, welcome to my blog!</div>
{% endif %}
{% if paginator.next_page == 2 %} tells Jekyll to check if the next page of the pagination is page2 (i.e. the current page is page1) and show the specified content.
This works best of all:
{% if paginator.previous_page %}
{% else %}
<div id="welcome">Hello, welcome to my blog!</div>
{% endif %}
Untested, but {% if paginator.previous_page == 0 %} might work as well.
The best way to to is :
{% if paginator.page==1 %}
// Do something here
{% endif %}
I've just recently started using Github to host my blog (using Jekyll and Liquid). However, I'm having an issue which I can't currently fix. The issue could be hacked/solved if I was able to detect which "page" or "url" the user was visiting.
Something like:
{% if user_is_currently_at_this_url %}
{{ display something }}
{% else %}
{{ display something else }}
{% endif %}
Is this possible? Is there any other way around this issue?
page.url is the URL of the current page, without the host (e.g. /index.html), as documented in Page Variables. So, in this case:
{% if page.url == "/index.html" %}
something
{% else %}
other thing
{% endif %}
(However, I don't think you need this any more, your other problem is probably solved. :) )
You could use {{ canonical_url }}