Search code examples
blogsjekyllstatic-pages

Add Links and Excerpts to other Posts on my Blog in the Bottom of each Post


I'd like to add to the bottom of my post few (2-4) links to other posts. At first I thought they should be correlated but I guess random ones work as well.

I'm using Jekyll and am looking for a plugin or an easy way to just reference other (relevant) content which I've written in the bottom of the blog in a more automated way. The idea would be to have 2-4 divs with the titles and excerpts of other blog posts. Relevancy could be concluded simply by checking the tags, random ones are also ok for the beginning.

I have no idea how to do this in Jekyll directly (except manually adding those items). I thought of a JavaScript solution, where I could get the feed.xml (or maybe create a custom JSON file with the necessary information) and just display those items, but I can't be the first person looking for this, or?

Isn't there anything like this out there already? - Maybe I was just looking wrong.


Solution

  • Ok, I was looking wrong, the variable in jekyll which contains those information is site.related_posts.

    However, there seems to be some problem with it, when using in GitHub Pages, which makes it mostly useless. Many people just get the list of post in chronological order.

    There is an issue which gives a summary of possible solutions: https://github.com/johnotander/pixyll/issues/41

    Wenli Zhang wrote a quite extensive post about this, the template from Wenli Zhang's post which should work for GitHub Pages:

    {% assign hasSimilar = '' %}
    {% for post in site.related_posts %}
        {% assign postHasSimilar = false %}
        {% for tag in post.tags %}
            {% for thisTag in page.tags %}
                {% if postHasSimilar == false and hasSimilar.size < 6 and post != page and tag == thisTag %}
                    {% if hasSimilar.size == 0 %}
                    <h4>Similar Posts</h4>
                    <ul>
                    {% endif %}
                    <li class="relatedPost">
                        <a href="{{ site.url }}{{ post.url }}">{{ post.title }}
                        {% if post.series %}
                            (Series: {{ post.series }})
                        {% endif %}
                        </a>
                    </li>
                    {% capture hasSimilar %}{{ hasSimilar }}*{% endcapture %}
                    {% assign postHasSimilar = true %}
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% endfor %}
    {% if hasSimilar.size > 0 %}
        </ul>
    {% endif %}