Last active
December 17, 2018 17:55
-
-
Save andrewlaskey/bc77a2954eb8db7823efa4a0d6ef3d08 to your computer and use it in GitHub Desktop.
Adding stick, featured articles to a Shopify blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="blog-main"> | |
{% comment %} Get all the articles for the blog {% endcomment %} | |
{% assign all_articles = blog.articles %} | |
{% assign posts_per_page = section.settings.posts_per_page %} | |
{% assign feature_article_1 = articles[section.settings.feature_article_1] %} | |
{% comment %} | |
Start pagination as you normally would. This is to be sure the correct | |
page nav links show up without having to modify anything about them. | |
{% endcomment %} | |
{% paginate blog.articles by posts_per_page %} | |
{% comment %} | |
Using the page number multiplied by the post per page we figure out | |
the upper and lower limits of blog article indexes that will be shown for that page. | |
{% endcomment %} | |
{% assign current_page = paginate.current_page %} | |
{% assign lower_limit = current_page | minus: 1 | times: posts_per_page %} | |
{% assign top_limit = lower_limit | plus: posts_per_page %} | |
{% comment %} Start a counter for blog articles {% endcomment %} | |
{% assign index = 0 %} | |
<div class="blog-content"> | |
{% comment %} | |
If we have a feature article, increase the index to offset the article's position in | |
the all_articles array | |
{% endcomment %} | |
{% if feature_article_1 %} | |
{% assign index = index | plus: 1 %} | |
{% if current_page == 1 %} | |
{% include 'post-tile' with { | |
article: feature_article_1 | |
} %} | |
{% endif %} | |
{% endif %} | |
{% comment %} Loop through all the blog articles rather than the pagination articles {% endcomment %} | |
{% for article in all_articles %} | |
{% comment %} | |
If the article's index is within the pagination limits and not a feature article display it | |
{% endcomment %} | |
{% if index >= lower_limit and index < top_limit %} | |
{% if feature_article_1.id != article.id %} | |
{% include 'post-tile' with { | |
article: article | |
} %} | |
{% endif %} | |
{% endif %} | |
{% comment %} | |
Increase the counter for articles that aren't the feature article | |
{% endcomment %} | |
{% if feature_article_1.id != article.id %} | |
{% assign index = index | plus: 1 %} | |
{% endif %} | |
{% endfor %} | |
</div> | |
{% comment %} Add regular pagination {% endcomment %} | |
{% include 'pagination' %} | |
{% endpaginate %} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment