English 中文(简体)
Having difficulties with Jekyll / Liquid
原标题:

I m tring to do a loop for Nav links below my posts. This is going into the _layout of posts.html

I can t get the link to not show if the post is the last or the first. Any help would be awesome.

{% for post in site.posts %}

    {% if post.previous != forloop.last %} 
        ← Last
    {% elsif post.next != forloop.first %} 
        Next →
    {% endif %}

{% endfor %}
最佳回答

I had better luck using page.next/previous

    {% if page.previous %} 
        <a rel="prev" href="{{ page.previous.url }}">&larr; Older</a>
    {% endif %}
    {% if page.next %} 
        <a rel="next" href="{{ page.next.url }}">Newer &rarr;</a>
    {% endif %}
问题回答

Change your if statements to just check if post.previous exists.

{% if post.previous %}
<span class="page-nav-item">
    <a rel="prev" href="{{ post.previous.url }}" title="View {{ post.previous.title }}">&larr; View previous article</a>
</span>
{% endif %}
{% if post.next %}
<span class="page-nav-item">
    <a rel="next" href="{{ post.next.url }}" title="View {{ post.next.title }}">View next article &rarr;</a>
</span>
{% endif %}

Add images to your link background. For images to show up just add image: [image location] in your YAML front matter

<div class="postNav clearfix">
    {% if page.previous.url %} 
      <a class="prev{% if page.previous.image %} image{% endif %}" href="{{ page.previous.url }}"><span>&laquo;&nbsp;{{ page.previous.title }}</span>
      {% if page.previous.image %} 
        <img src="{{  /assets/blog-img/  |  append: page.previous.image }}" alt="">
      {% endif %}
    </a>
    {% endif %}  
    {% if page.next.url %}  
      <a class="next{% if page.next.image %} image{% endif %}" href="{{ page.next.url }}"><span>{{ page.next.title }}&nbsp;&raquo;</span>
      {% if page.next.image %} 
        <img src="{{  /assets/blog-img/  | append: page.next.image }}" alt="">
      {% endif %} 
      </a>
        {% endif %}
 </div> 




相关问题
Using liquid with haml

I m planning to develop a CMS with ruby/rails. One of the main key features that I m planning is to give the user to edit their layout (I m planning to do this through liquid) meanwhile i have red ...

Extending Jekyll and Liquid to parse post s content

My blog, powerred by Jekyll, serves out a Atom feed. --- layout: nill rooturi: http://stefan.artspace44.com --- <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/...

Liquid templates - accessing members by name

I m using Jekyll to create a new blog. It uses Liquid underneath. Jekyll defines certain "variables": site, content, page, post and paginator. These "variables" have several "members". For instance, ...

How to make will_paginate work with liquid

I managed to make a little hack to make will_paginate work with liquid: http://gist.github.com/426737 I wonder if this is safe? Ideas?

undefined method `call for LiquidView:Class

I trying to use Liquid template engine plugin , but I m getting the following error while controller tries to render a .liquid template . "undefined method `call for LiquidView:Class" ...

Liquid Templates Not Parsing!

Im trying to use Liquid Template Language in my Rails Application, i ve watched Ryan Bates video over at rails cast, i pretty much follow the instructions but it just doesnt seem to work! When I try ...

Having difficulties with Jekyll / Liquid

I m tring to do a loop for Nav links below my posts. This is going into the _layout of posts.html I can t get the link to not show if the post is the last or the first. Any help would be awesome. {% ...

热门标签