Show blog post title in meta information - html

I'm working on Search Engine Optimization for my Jekyll site, and I am running into an issue where I am unable to set meta information.
...
<meta name="og:title" content="{{ seo_title }}" />
...
<!-- now in my for post-loop: -->
{% for post in site.posts %}
<li class="post-link">
<a class="post-title" href="{{ post.url }}">
<span class="post-date">{{ post.date | date_to_string }}</span>
{{ assign seo_title = post.title }}
{{ post.title }}
</a>
</li>
{% endfor %}
I'm assigning sel_title to the post title, but it's not showing up in my meta information! I just get <meta name="og:title" content="" />
I've also tried adding {{ assign seo_title = page.title }} inside of my post.html post layout to no avail using {{ page.seo_title }} {{ post.seo_title }} and {{ seo_title }}
Now, obviously this really isn't what i want, because logically - after this for loop, it would set it to whatever the last post title was, but i can't even get that to display. Ideally what I would like, is for it to show up for the post.
You can view the page here where I want it to show up.
Where am I going wrong? How can I use my post information to fill my SEO meta information?

Had a look at your github repo. You are using only one basic layout for both index and posts. In your _posts, the YAML Front Matters is getting processed first time (and only once) when you do {% include post.html %}. But then you pass it on to the default layout and you cannot pass the YAML variables from the post through also.
One typical pattern you can adopt is split the default layout and create partial html files in the _includes folder i.e header.html and footer.html. You can then use them to create your default and post layouts. That way you can pass on the relevant YAML Front Matter variables in the post to the header.

Related

Posts not showing correctly on jekyll (multiple site) blog -- only post code

I have a personal website built with jekyll and hosted on Github pages. I am trying to add a sub-site blog within the same domain. For this, I set up a blog.md page and followed the instructions from this website: https://www.garron.me/en/blog/multi-blog-site-jekyll.html. The idea is that if I access http://<mydomain>.com it will go to my personal website, and if I go to http://<mydomain>.com/blog it will go to a different site also set up with jekyll.
My file structure is different than what they suggest in the link above. It is like this:
/personalwebsite
config.yml
index.md
(other personal website pages).md
blog.md
/_site
/_layouts
/_posts
My index.md page is completely customized, and I wrote my own layout for that website. It is a static site and everything in _posts is ignored by it. My blog.md page is also on the root folder and it changes according to _config.yml. I am trying to use Github jekyll themes for it. The theme loads, but instead of showing the posts, it shows the code:
This is what blog.md looks like:
---
layout: blog
title: the blog
permalink: blog
---
{% raw %}
{% for post in site.posts %}
{% if post.categories contains 'blog' %}
<div class="post">
<h3 class="title">{{ post.title }}</h3>
<p class="meta">Date: {{ post.date }}</p>
<div class="entry">
{{ post.content | strip_html | truncatewords: 100 }}
</div>
</div>
{% endif %}
{% endfor %}
{% endraw %}
And this is what a post looks like:
---
layout: post
title: New test
category: blog
---
This is a test post
If I remove the {% raw %} parts in blog.md, the posts show up like this:
I have already checked that my posts are in the right place, the category parameter is filled in, the dates and post filenames are properly formatted. What am I doing wrong? Jekyll does not show any error messages other than a Github metadata warning:
GitHub Metadata: No GitHub API authentication could be found. Some fields may be missing or have incorrect data
blog.md is a markdown file.
In markdown a four space indentation represents code or preformatted text.
Kramdown will wrap this code in <pre> tag, resulting on what you actualy see on your site.
If you remove your indentation (or keep it under 4 spaces), your problem is solved.
{% for post in site.posts %}
{% if post.categories contains 'blog' %}
<div class="post">
<h3 class="title">{{ post.title }}</h3>
<p class="meta">Date: {{ post.date }}</p>
<div class="entry">
{{ post.content | strip_html | truncatewords: 100 }}
</div>
</div>
{% endif %}
{% endfor %}

jekyll blog permalink breaks and yields "404 not found"

I have started a small website with a few pages and a couple of blog posts. It is hosted on my organization's server and I ftp'ed all contents of _site/ directory into a subdirectory of the website. Hence the Jekyll site is at http:// myusername.foobar.foo/thiswebsite/ .
In my _config.yml
baseurl: "/thiswebsite"
url: "http:// myusername.foobar.foo"
Now all pages show up correctly. But blog posts don't.
In YAML front matter of each blog post:
permalink: /:title.html
Then I ended up generating links on index.html page to blog posts at http:// myusername.foobar.bar/blog-title.html but the actual blog posts are found at http:// myusername.foobar.bar/thiswebsite/blog-title.html. So if people click on the links found on index.html they will see 404.
On index.html I have:
{% for post in site.posts %}
<h2>{{ post.title }}</h2>
<blockquote>
{{ post.excerpt }}
</blockquote>
{% endfor %}
I would have thought {{ post.url }} would automatically insert correct URL for the posts, but apparently that's not happening. :(
Where did I screw up?
(Jekyll version 3.1.2)
Note: blank space after http:// is intentional as StackExchange thinks I'm posting links and that's apparently not allowed. In my actual markdown and html they are proper URLs.
Link to a collection item (post are collections items) or pages :
eg : {{ post.title }}
or {{ post.title }}
And it's the same for resources links (image, script, css, ...).
eg : <link rel="stylesheet" href="{{ "/css/main.css" | prepend: site.baseurl }}">
or <script src="{{ site.baseurl }}/js/script.js"></script>

Jekyll - display a random chosen post in index

I would like to display a randomly chosen post in the front page of my Jekyll site.
Do you have any idea how I could loop and chose a random post each time the page is loaded ?
This is the index that I have at the moment.
---
layout: default
title: Home
---
<h1 class="content-listing-header sans">Posts</h1>
<ul class="content">
{% for post in site.posts %}
<li class="listing">
<hr class="slender">
<h4 class="contrast">{{ post.title }}</h4>
<span class="smaller">{{ post.date | date: "%B %-d, %Y" }}</span> <br/>
<div>{{ post.excerpt }}</div>
</li>
{% endfor %}
Thanks
Jekyll generates static files. You can chose a random post to be inserted in your home page, but this page will be static and random post will only be changed when you site is generated.
{% assign random = site.time | date: "%s%N" | modulo: site.posts.size %}
<h1>{{ site.posts[random].title }}</h1>
As liquid has no random tag, you can mimic randomness based on time.
See https://stackoverflow.com/a/28323813/1548376
The only way to load a different post on each reload is to do it with javascript. And here it will become complicated.
you will need to create a posts list for javascript to choose from,
you will have to generate specific page for each post with only post's html in it. No head, navigation and so on. And this can only be accomplished with a Jekyll generator plugin.

Add a project page to site.posts on a Jekyll blog

I have a Jekyll blog where the main page is a bunch of links to posts, but where I also want to include a link to a project's gh-pages page and I want the list to stay sorted by date. Right now, I'm just manually inserting it at the top of the page.
<ul class="posts">
<li>
<span class="post-date">Jul 8, 2015</span>
<a class="post-link" href="/QuisCustodiet/">The Most Influential Works, According to TvTropes</a>
</li>
{% for post in site.posts %}
<li>
<span class="post-date">{{ post.date | date: "%b %-d, %Y" }}</span>
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
This looks okay, but it will break in a week or two when I make another post. Is there a way to create another "post" and insert it into the list of site.posts in a way that it stays sorted? Is there some other much better way of doing this that I don't know about?
If I understand your problem right, you want to have some "special project posts" in your list of posts that are only links to another specific project site but have a date and therefore can be sorted together with the other posts.
Here is what I came up with:
You create an empty Post in _posts/ with front matter like this:
---
layout: post
title: "Project Example"
customlink: http://www.example.org
date: 2015-07-12 12:50:25
---
The customlink attribute is for the link to your project page. In your html, where you list all your posts, you make an if-statement to check for the attribute and handle it properly. Like this for example:
<ul>
{% for post in site.posts %}
<li>
{% if post.customlink %}
{{ post.title }}
{% else %}
{{ post.title }}
{% endif %}
</li>
{% endfor %}
</ul>
Your project post is treated as your other posts and sorted with them by date, but its link will direct the user to your project page.
Jekyll will still create the html for this post and serve it like all the other posts under a specific path. To prevent this you could add an empty permalink attribute in the posts front matter like permalink: "" and your post would lie under the root of your blog. Jekyll would still create a ".html" file for this post in the _site folder.
You need to create the posts for every external project manually but I do not know a better solution.

{{ content }} Liquid tag not showing blog posts

Link to repo: https://github.com/AlvSovereign/AlvSovereign.github.io
I am coding my portfolio site, with a link to my blog. The fully processed portfolio site sits in _site/index.html which contains a link to the blog (fully processed link exists in _site/blog.html)
Blog.html at the root of the repo has in the front matter a layout of "bl", which is defined in my layouts folder under "bl.html".
"bl.html" is the layout I want for my blog page, which contains my includes etc. It also contains the {{ content }} liquid tag.
If I am thinking about this the right way, my posts are being parsed (I think this is the right terminology) into "post.html" - which has the front matter "layout: bl", which then parsed into "bl.html", and that this is so as they both have {{ content }} in each files.
Now all my posts are showing correctly (i think) within _site/(year)/(month) etc. However, they are not ending being parsed through my "blog.html" file, and then visible on my webpage. How I know this is that the fully processed "blog.html" file does not have any of the posts in them.
What will I need to do to solve this issue?
If it helps, I use Prepros for LiveReload, and using LANYON template, http://lanyon.getpoole.com/
{{ content }} refers to all the content that in the file that is being converted to HTML code.
For example, if you are passing a blog post on 'What is Jekyll' to any layout that has the {{ content }} tag, all text other than the YAML front matter will be put in that place. Basically the content of the post. You can also limit the scope by using {{ post.content }}, see Variables for more info here.
So the Liquid {{ content }} variable works as intended.
Looking at your website, I believe you're trying to display all the posts in your blog.html file, so what you're actually asking about is why aren't my posts being displayed (not parsed) in your blog.html file, hence not visible on your webpage.
The reason for that is because you shouldn't use {{ content }} tag for that, what you want instead is something like this snippet of code as mentioned in the Jekyll docs, that will grab all your posts, and display them by post title, as well as a link to that post. Here's the code to display a list of your posts. (taken from the website I linked you)
<ul>
{% for post in site.posts %}
<li>
{{ post.title }}
</li>
{% endfor %}
</ul>
So just swap out {{ content }} in your bl.html layout with that code snippet and you've got what you want.
Additionally, if you also want to display the content/excerpt in the list of posts, just add the liquid tag {{ post.content }} or {{post.excerpt}} like so:
<ul>
{% for post in site.posts %}
<li>
{{ post.title }}
{{ post.content }} /* or post.excerpt */
</li>
{% endfor %}
</ul>
Of course, edit the HTML accordingly for your preference. Read the documentation on how to set up post excerpts here, which uses the <!--more--> comment separator for excerpts.
I strongly recommend reading the JekyllRb documentation for more information, it's a lot to read and not always easily understandable, but keep referring to it and you'll understand more in no time.