1
votes

i am migrating my website made with php and wordpress in jekyll. everything is going smooth except for some problems i'm having with the blog section.

here what my blog/index.html looks like:

---
layout: posts
---    
{% for post in paginator.posts %}
<h2><a href="{{ post.url }}">{{ post.title }}</a></h2>
<p>
    <span class="date"><i class="fa fa-date"></i> {{ post.date | date: "%m-%d-%Y"}}</span>
</p>

    {{ post.excerpt }}
    <a class="more-link" href="{{ post.url }}">Read more...</a>

{% endfor %}

what i would like to happen is that the html of the layout: posts repeats for every {{ post.excerpt }} i have. instead, it just wraps all post excerpt into that single html bit. here's the layout:

<div class="page-container">
    <div class="sub-container blog">
        <h1>Blog</h1>
        <div class="entry">

            {{ content }}
        </div>
        <!-- Pagination links -->
        <div class="pagination">
            <ul>
                {% if paginator.previous_page %}
                {% if paginator.previous_page == 1 %}
                <li>
                    <a href="/blog/">Prev</a>
                </li>
                {% else %}
                <li>
                    <a href="/blog/{{ paginator.previous_page }}">Prev</a>
                </li>
                {% endif %}
                {% else %}
                <li>
                    <span class="disabled">Prev</span>
                </li>
                {% endif %}
                {% if paginator.page == 1 %}
                <li>
                    <span class="active">1</span>
                </li>
                {% else %}
                <li>
                    <a href="/blog/">1</a>
                </li>
                {% endif %}
                {% for count in (2..paginator.total_pages) %}
                {% if count == paginator.page %}
                <li>
                    <span class="active">{{ count }}</span>
                </li>
                {% else %}
                <li>
                    <a href="/blog/{{ count }}">{{ count }}</a>
                </li>
                {% endif %}
                {% endfor %}
                {% if paginator.next_page %}
                <li>
                    <a href="/blog/{{ paginator.next_page }}">Next</a>
                </li>
                {% else %}
                <li>
                    <span class="disabled">Next</span>
                </li>
                {% endif %}
            </ul>
        </div>
    </div>
</div>

is there a for loop i have to create with post excerpts? what's the syntax? i have no idea how to achieve this.

thanks!

2

2 Answers

1
votes

Why did you put your loop with excerpt in one file and pagination in an other ?

This post template will only be used by blog index and pagination pages, so you can merge everything in your blog/index.html and do :

---
layout: default
---
<div class="page-container">
    <div class="sub-container blog">
        <h1>Blog</h1>
        <div class="entry">
        {% for post in paginator.posts %}
            <h2><a href="{{ post.url }}">{{ post.title }}</a></h2>
            <p><span class="date"><i class="fa fa-date"></i> {{ post.date | date: "%m-%d-%Y"}}</span></p>
            {{ post.excerpt }}
            <a class="more-link" href="{{ post.url }}">Read more...</a>
        {% endfor %}

        </div>
        <!-- Pagination links -->
        <div class="pagination">
            <ul>
                ... pagination code here
            </ul>
        </div>
    </div>
</div>
0
votes

actually i just needed to move the opening and the closing of the for-loop outside div.entry like so:

{% for post in paginator.posts %}
<div class="entry">

    <h1><a href="{{ post.url }}">{{ post.title }}</a></h1>
    <p>
        <span class="date"><i class="fa fa-calendar"></i> {{ post.date | date: "%m-%d-%Y"}}</span>
    </p>
    {{ post.excerpt }}
    <a class="more-link" href="{{ post.url }}">Read more...</a>

</div>
{% endfor %}