Sorting posts by category using Jekyll works locally but not on Github pages? - html

I have posts in Jekyll sorted by categories that wont display on github pages. The yaml font matter in the post has the categories set to CSS and design but don't display on the category page with the code below:
{% for post in site.categories.CSS %}
{% if post.url %}
<a id="h1a" href="{{ post.url }}">{{ post.title }}</a>
<p id="date">{{ post.author }} • {{ post.date | date: "%b %-d, %Y" }}</p>
<div id="excerpt">{{ post.excerpt }} </div>
<div id="readmore">Read More</div>
{% endif %}
{% endfor %}
It works locally, and the URL path (/css/design/2016/01/10/responsive-web-design-css-viewport.html) shows that the categories are there, but does not display in the link above. Here is my repository, the code above can be found in the css folder of the root directory.

Jekyll 3.x uses categories "as is" : CSS stays CSS.
Jekyll 2.x is down-casing categories : CSS becomes css.
So, on Github pages site.categories.CSS == nil
In order to work locally in Github pages configuration, you can follow install instructions here.

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 %}

limit jekyll posts to specified folder

I am using Jekyll to build a web site,
I want to have some folder structure like
root
- _posts
Kinds of MD files
Index.html
- v1.0
- _posts
Kinds of MD files
Index.html
I want to build a web site, the URL like http://host/root/ points to current version document,the URL like http://host/root/v1.0 points to version 1.0 document.
In both index.html, I use code like below,
{% assign sortedCategories = site.categories | sort %}
{% for category in sortedCategories %}
<h1 class="post-title" id="{{ category[1] | replace:' ','-' | replace:'.','' }}">{{ category[1] | join: "/" | upcase }}</h1>
<ul class="post-list">
{% for posts in category %}
{% for post in posts reversed %}
{% if post.title != null%}
<li>
<h2 class="post-title" id="{{ post.id | remove_first:'/'}}">{{ post.title }}</h2>
<article class="post-content">
{{ post.content }}
</article>
</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
{% endfor %}
The issues I met is,
As both _post folder has similar posts and category, in the http://host/root/, each post is shown twice.
In URL http://host/root/, it has an additional category which contains all the posts (in both folder _posts and v1.0/posts)
The same issues for URL http://host/root/v1.0
I want to know how I can restrict the site.categories to only search specified folder? Or there is some other suggestions on this?
thanks
posts can not be limited to a particular folder as it is site wide. You can create custom collection. Suppose if you have v1, v2 and root(pointing to v2). You can use the same collection name for v2 and root.
As mentioned by #ganesshkumar, use jekyll collections for this

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.

Why isn't {{ page.date }} working in my jekyll install?

I installed my old blog on a github pages site, transferring it to jekyll from textpattern. It's in a sub-directory called /journal. The individual posts show the post's date no problem.
On the /journal/index.html page, {{ page.date | date: "%x" }} does not work at all.
What do I need to add so that the date will show up on the blog's index page?
If you are looping over your posts in index.html then you'll want to use the post's date instead of page.date. It'll look something like this:
{% for post in paginator.posts %}
{{ post.date | date: "%x" }}
{% endfor %}

After upgrade, Jekyll site no longer generates blog index page

I recently upgraded to Jekyll 2.0.3. I have a site with a blog index page:
_layouts/
[ layouts]
_posts/
[ posts ]
blog/index.html
index.html
other-stuff.html
And I also have _plugins, _includes etc.
All pages apart from blog/index.html generate fine. blog/index.html gives me the bare source in the generated folder:
---
layout: blog
title: My blog
---
{% for post in site.posts limit:5 %}
<div class="blog-summary">
<h2>{{ post.title }}</h2>
<p class="blog-post-date">{{ post.date | date: "%B %d, %Y" }} in {{ post.category }}</p>
{{ post.content | postmorefilter: post.url, "Read more..." }}
</div>
{% endfor %}
I tried:
Simplifying the blog/index.html to some plain text. The page is just the same, but with the text instead
Using -t switch on the build command. Nothing is output
Using the watch command. Again, no different.
How can I debug this?
Save the page without BOM.
Chances are the BOM is appearing before the initial front-matter, causing Jekyll to pass over it.