<section id="article">
<h3>Recent posts</h3>
<ul>
{% for post in site.posts %}
<li>» {{ post.title }}</li>
{% endfor %}
</ul>
</section>
This is my codes for all articles, how can I control the number of posts, and just show 10 posts in the section?
This is the way to do it.
{% for post in site.posts offset: 0 limit: 10 %}
<section id="article">
<h3>Recent posts</h3>
<ul>
{% for post in site.posts limit:10 %}
<li><a href="{{ post.url }}">
{% endfor %}
</ul>
</section>
Try this one. This code show 10 most recent post like recent post widget.
I'd go about it another way.
{% for post in site.posts limit:1 %}
{% if forloop.last %}
<li>
{{ post.title }}
</li>
{% endif %}
{% endfor %}
I've included an if logic tag with a forloop.last so it would only display the last, most recent post. The output would only be one post since I've also included { limit:1 }.
Related
I have a tags.md which contain the code below, It list the tags from my _site/post.md
---
layout: default
title: Tags
---
{% for tag in site.tags %}
<h2>{{ tag[0] }}</h2>
<ul>
{% for post in tag[1] %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
{% endfor %}
And I also have a Collections for work stuff, like work.md in the jekyll root directory and the post in there is inside _work/
How can I make the tags.md list the tags from both _posts/*.md and _work/*.md?
I realize I can't do something like this
{% for tag in site.tags %}
{% for tag in work.tags %}
<h2>{{ tag[0] }}</h2>
<ul>
{% for post in tag[1] %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
{% endfor %}
{% endfor %}
Using site.documents should be your way to go, see https://jekyllrb.com/docs/variables/
site.documents
A list of all the documents in every collection.
Internally, posts are also treated as a collection here.
The first line below groups all documents by tag:
{% assign docs_by_tags = site.documents | group_by: 'tags' %}
{% for tag in docs_by_tags %}
<h2>{{ tag.name }}</h2>
<ul>
{% for item in tag.items %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
{% endfor %}
BUT the result shows quoted tag names, which is not what we want.
A better solution to group arrays
Someone else already found a good way to group arrays in posts and collections back in 2015, see https://github.com/mushishi78/jekyll-group-by-array/blob/master/group-by-array.html - the project's readme has some sample code using the include file.
The code "extracts" tags or other attributes and pushes data to arrays before data is returned to be displayed.
The solution with both post and collection tag
Just use site.documents in the include as first parameter
{% comment %}based on https://github.com/mushishi78/jekyll-group-by-array{% endcomment %}
{% include group-by-array.html collection=site.documents field='tags' %}
<ul>
{% for tag in group_names %}
{% assign posts = group_items[forloop.index0] %}
<li>
<h2>{{ tag }}</h2>
<ul>
{% for post in posts %}
<li>
<a href='{{ site.baseurl }}{{ post.url }}'>{{ post.title }}</a>
</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
Reversing the posts order in the list
To reverse the post order under the tag, you can add a reversed to the for loop above:
{% for post in posts reversed %}
hello everyone i am new to github,jekyll and ruby and maybe this question is already answered but being a newbie it's hard for me to resolve the problem.
i am trying to put multiple paginations on a single page i.e. say i have two
authors posting their content on the blog so i created a division for each of them and want pagination for each of them individually.So the current code is something like this:
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
{% for post in paginator.posts %}
{% if post.author contains "NAME OF AUTHOR 1" %}
<div class="post-preview">
<a href="{{ post.url }}">
<h2 class="post-title">
{{ post.title }}
</h2>
<h3 class="post-subtitle">
{{ post.description }}
</h3>
</a>
<p class="post-meta">Posted by {{ post.author }} {{ post.date | date_to_string }}</p>
</div>
<hr>
{% endif %}
{% endfor %}
<!-- Pager -->
{% if paginator.total_pages > 1 %}
<ul class="pager">
{% if paginator.previous_page %}
<li class="previous">
← Newer Posts
</li>
{% endif %}
{% if paginator.next_page %}
<li class="next">
Older Posts →
</li>
{% endif %}
</ul>
{% endif %}
</div>
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
{% for post in paginator.posts %}
{% if post.author contains "NAME OF AUTHOR2" %}
<div class="post-preview">
<a href="{{ post.url | prepend: site.url }}">
<h2 class="post-title">
{{ post.title }}
</h2>
<h3 class="post-subtitle">
{{ post.description }}
</h3>
</a>
<p class="post-meta">Posted by {{ post.author }} {{ post.date | date_to_string }}</p>
</div>
<hr>
{% endif %}
{% endfor %}
<!-- Pager -->
{% if paginator.total_pages > 1 %}
<ul class="pager">
{% if paginator.previous_page %}
<li class="previous">
← Newer Posts
</li>
{% endif %}
{% if paginator.next_page %}
<li class="next">
Older Posts →
</li>
{% endif %}
</ul>
{% endif %}
</div>
Also under _config.yml paginate is set to 3.
I have used jekyll with bootstrap(if am correct not jekyll-bootstrap) and followed a pretty simple tutorial ,also the file structure is also very simple.
thanks for your help i have read many documentation and many posts(always bound to screwup) before posting this so thanks to everyone.
Also,
the blog is hosted at http://neorblog.in and github repositories at https://github.com/neortls007idev/Blog
Also the repo is not currently commited as per the above code.
Pagination is for all posts. You cannot have a pagination for posts where author == NeoR.
You still have the solution to create a generator plugin. And yes, github pages doesn't accept foreign plugin for security reasons.
But, simply pushing to github is not the only workflow available. You can try this.
I've got the following code in my index.html for Jekyll. I'm trying to find a way to link the categories associated with each post to the actual post themselves. So, if a post contains the category "travel" I want to click on a link that says "travel" which will bring me to all posts categorized as such.
<ul class="post-list" style="list-style-type: none;">
{% for post in paginator.posts %}
{% unless post.categories contains 'portfolio' %}
<li>
<h3>{{ post.title }}</h3>
<span class="post-meta">{{ post.date | date: "%c" }}</span>
Filed In:
{% unless p.categories == empty %}
{% for categories in post.categories %}
{{ categories }} //problem area
{% endfor %}
{% endunless %}
{{ post.excerpt }} Find out more...<br><br>
</li>
{% endunless %}
{% endfor %}
</ul>
Figured it out. For anyone else wondering how to do the same, first setup a categories.html page in your root directory. This page will list all posts that meet a specific category. It does by turning the category names into named anchor slugs as such <a href="#{{ category | first | remove:' ' }}" and then the preceding code creates the actual named anchor div which displays the post associated with that category. Finally, under the page or section where you want to display the list of categories, you present the final bit of code which links to the named anchor section in your categories.html page.
First piece of code to go into Categories.html:
<h2>Posts by Category</h2>
<ul>
{% for category in site.categories %}
<li><strong>{{ category | first }}</strong></li>
{% if forloop.last %}
{% else %}
{% endif %}
{% endfor %}
</ul>
Second piece of code to go into Categories.html:
{% for category in site.categories %}
<div class="catbloc" id="{{ category | first | remove:' ' }}">
<h2>{{ category | first }}</h2>
<ul>
{% for posts in category %}
{% for post in posts %}
{% if post.url %}
<li>
<a href="{{ post.url }}">
<time>{{ post.date | date: "%B %d, %Y" }}</time> -
{{ post.title }}
</a>
</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>
{% endfor %}
Third piece of code to go where you want to display your named anchor linked categories:
Filed In:
{% unless p.categories == empty %}
{% for categories in post.categories %}
{{ categories }}
{% endfor %}
{% endunless %}
Use the following CSS to prevent the sections from displaying prematurely before you click on them:
.catbloc:not(:target){
display: none;
}
Hope this helps!
In shopify I am using liquid templating to get blog posts which are related to products by their tags, like so:
<ul>
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
<li><p>{{ article.title }}</p></li>
{% endif %}
{% endfor %}
</ul>
However, if there are no related posts, I would like to display a message such as "No related posts!"
My question is how would I do that? I have contemplated trying to get the articles into an array and testing if it is empty, but I am having difficulty finding out how this is done.
Thanks!
Try something like this:
{% assign related_posts = "" %}
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
{% capture post %}
<li><p>{{ article.title }}</p></li>
{% endcapture %}
{% assign related_posts = related_posts | append:post %}
{% endif %}
{% endfor %}
{% if related_posts.size > 0 %}
<ul> {{ related_posts }} </ul>
{% else %}
No related posts!
{% endif %}
<ul>
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
<li><p>{{ article.title }}</p></li>
{% else %}
<li>No related blog posts!</li>
{% endif %}
{% endfor %}
</ul>
I am new using Jekyll and Jekyll-Bootstrap.
I have found this for filtering by category:
<ul class="posts">
{% for post in site.posts %}
{% if post.categories contains 'demography' %}
<li><span>{{ post.date | date_to_string }}</span> » {{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
When I try to combine tags and categories it doesn't work:
<ul class="posts">
{% for post in site.posts %}
{% if post.categories contains 'demography' and post.tags contains 'R' %} %}
<li><span>{{ post.date | date_to_string }}</span> » {{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
Any idea?
Thanks in advance!
You have a %} too much in line #3.
Other than that, it should work fine.