Blog Sidebar ~ blogs[blog.handle].articles

Highlighted
Shopify Expert
682 0 67

Hey,

I am trying to create a blog archive-by-month thing. I have this repeated (manually) in my sidebar include):

{% for article in blog.articles %}
 {% capture date %}{{ article.created_at | date: "%B %Y" }}{% endcapture %}
 {% unless date != 'September 2012' %}
  <li>&raquo; {{ article.title | link_to: article.url }}</li>
 {% endunless %}
{% endfor %}

That works fine (although help with automating it rather than having to copy/paste it every month would be appreciated :)

However, if I replace blog.articles with blogs[blog.handle].articles (I am now trying to paginate as well), it doesn't work as expected. In fact, I get nothing returned at all. Same with blog.all_articles. When I try blog.all_articles_count I also get nothing returned.

Blog is here.

Any ideas?

I'm a million different people
1 Like
Shopify Staff (Retired)
Shopify Staff (Retired)
2000 0 117

Ugh, yeah it looks like you're using the snippet I've been using for a few years. There is no way to make this automatic, I've spent too much time trying various workarounds. If you indeed come up with something Rick then please post back.

On the archives and the articles not showing up I would make sure that you are running a higher pagination for your archives block, paginate your archives by a very high number and try that.

I too am waiting for true blog archives as well.

http://shopifyplus.com ::: http://twitter.com/bacchus
0 Likes
Shopify Expert
682 0 67

Re: articles not showing up, the wiki for "blogs[blog.handle].articles" says:

Returns a collection of all of this blog's articles regardless of pagination

... and for "blog.all_articles" and "blog.all_articles_count" it says

Returns [a collection / the count] of all of this blog's articles (ignoring pagination).

I now have the blog itself paginated by 24, which splits it onto 2 pages. The sidebar loops look like this:

{% for article in blogs[blog.handle].articles limit: 48 %}
{% capture date %}{{ article.created_at | date: "%B %Y" }}{% endcapture %}
 {% unless date != 'February 2012' %}
  <li>&raquo; {{ article.title | link_to: article.url }}</li>
 {% endunless %}
{% endfor %}

But, on blog page 2 you can see those sidebar loops are definitely not ignoring pagination.

What is really throwing me off is why {{ blog.all_articles_count }} isn't returning anything at all... even if I remove the pagination object... (I swear I splelled it right!)

I'm a million different people
0 Likes
Shopify Expert
682 0 67

I got this working:

{% capture month %}{{ 'now' | date: "%B" }}{% endcapture %}
  {% for i in (1..3) %}
    {% case month %}
       {% when 'January' %}{% assign previous_month = 'December' %}
       {% when 'February' %}{% assign previous_month = 'January' %}
       {% when 'March' %}{% assign previous_month = 'February' %}
       {% when 'April' %}{% assign previous_month = 'March' %}
       {% when 'May' %}{% assign previous_month = 'April' %}
       {% when 'June' %}{% assign previous_month = 'May' %}
       {% when 'July' %}{% assign previous_month = 'June' %}
       {% when 'August' %}{% assign previous_month = 'July' %}
       {% when 'September' %}{% assign previous_month = 'August' %}
       {% when 'October' %}{% assign previous_month = 'September' %}
       {% when 'November' %}{% assign previous_month = 'October' %}
       {% when 'December' %}{% assign previous_month = 'November' %}
    {% endcase %}
    <dl>
      <dt>{% if i == 1 %}This Month{% else %}{{ month }}{% endif %}</dt>
      <dd>
       <ul class="blog_links">
       {% for article in blogs[blog.handle].articles limit: 48 %}
	{% capture date %}{{ article.created_at | date: "%B %Y" }}{% endcapture %}
	{% if date contains month %}
	 <li>&raquo; {{ article.title | link_to: article.url }}</li>
	{% endif %}
       {% endfor %}
       </ul>
      </dd>
    </dl>
   {% assign month = previous_month %}
  {% endfor %}

... but still not with paginate (which is really baffling me): compare page 1 vs page 2

I'm a million different people
0 Likes
New Member
3 0 0

@rickydazla 

Thanks, this is great! We have an existing wordpress blog with 5+ years of history we can't port over (lack of shopify's categories and archives). This might be enough of a simulated archive that we could just post an extra link back to our old blog for anything beyond a few months. 

Love it, thanks!!

0 Likes
Shopify Partner
189 0 27

That is some superb stuff, @rickydazla, thanks.

I've been using Shopify for years and have only just encountered the inability to have blog archives. It really is something they need to add.

http://iamkeir.com
0 Likes
Tourist
3 0 1

Thanks @rickydazla for your code.

I have extended it a bit further and added Year checking, Do not Print Month Title if there are no Articles for that month. More than that I have added a Archives Drop down menu list all previous months posts (code might not that efficient as I am pretty new to Liquid)

        {% capture month %}{{ 'now' | date: "%B" }}{% endcapture %}
        {% capture month1 %}{{ 'now' | date: "%B" }}{% endcapture %}
        {% capture month2 %}{{ 'now' | date: "%B" }}{% endcapture %}
        {% capture month3 %}{{ 'now' | date: "%B" }}{% endcapture %}
        {% capture year %}{{ 'now' | date: "%Y" }}{% endcapture %}
        {% capture monthtitle %}{{ 'notset' }}{% endcapture %}
        {% for i in (1..3) %}
          {% case month %}
             {% when 'January' %}{% assign previous_month = 'December' %}
             {% when 'February' %}{% assign previous_month = 'January' %}
             {% when 'March' %}{% assign previous_month = 'February' %}
             {% when 'April' %}{% assign previous_month = 'March' %}
             {% when 'May' %}{% assign previous_month = 'April' %}
             {% when 'June' %}{% assign previous_month = 'May' %}
             {% when 'July' %}{% assign previous_month = 'June' %}
             {% when 'August' %}{% assign previous_month = 'July' %}
             {% when 'September' %}{% assign previous_month = 'August' %}
             {% when 'October' %}{% assign previous_month = 'September' %}
             {% when 'November' %}{% assign previous_month = 'October' %}
             {% when 'December' %}{% assign previous_month = 'November' %}
          {% endcase %}
          {% case i %}
        	 {% when 1 %}{% assign month1 = month | append:' ' | append: year %}
        	 {% when 2 %}{% assign month2 = month | append:' ' | append: year %}
        	 {% when 3 %}{% assign month3 = month | append:' ' | append: year %}
          {% endcase %}
        {% for article in blogs[blog.handle].articles limit: 48 %}
	        {% capture date %}{{ article.published_at | date: "%B %Y" }}{% endcapture %}
    	    {% if monthtitle == 'notset' and date contains month and date contains year %}
        		<dl>
          		<dt>{% if i == 1 %}This Month{% else %}{{ month }}{% endif %}</dt>
          		<dd>
            	<ul class="blog_links">
              	{% assign monthtitle = 'set' %}
            {% endif %}
            {% if date contains month and date contains year %}
              <li>&raquo; {{ article.title | link_to: article.url }}</li>
            {% endif %}
        {% endfor %}
        {% if monthtitle == 'set' %} 
             </ul>
            </dd>
          </dl>
        {% endif %}
         {% assign month = previous_month %}
         {% assign monthtitle = 'notset' %}
  		{% endfor %}
        <div class="nav" style="text-align: left !important">
        <ul class="main menu">
          <li style="padding-left: 0px !important;"><a title="Previous posts" href="" style="color: #9DA6AE !important;">Archives 
                    <span class="arrow">?</span></a> 
            		<ul style="left: 0px !important; width: 275px !important">
                      {% for article in blogs[blog.handle].articles limit: 48 %}
                      {% capture date %}{{ article.published_at | date: "%B %Y" }}{% endcapture %}
                      {% unless (date contains month1 or date contains month2 or date contains month3)) %}
                      <li>{{ article.title | link_to: article.url }}</li>
                      {% endunless %}
                      {% endfor %}
                    </ul>
                  </li>
            </ul>
        </div>        

 

1 Like
Shopify Expert
682 0 67

NOW we're cookin on gas! I tested out your code here - seems to work great:D 

I still wonder about my original pagination question, since my archive list seems to stop at 48 posts even though it is outside of the pagination loop..

I'm a million different people
0 Likes
Tourist
3 0 1

Yes, in my case also the Pagination gets applied for the Code on the Sidebar.

0 Likes
Tourist
3 0 1

@rickydazla, sorry for multiple replies, I understand the Pagination is getting set on our code while we move between Pages. But the limit [48] is set on our queries right, so I am seeing it as an expected behavior. In my case I do not want to show more than 48 entries other wise my Archive link list will become too lengthy and not navigable.

{%  for article in blogs[blog.handle].articles limit: 48 %}

 

0 Likes