d
Topic
Posts:
19
July 02, 2013

Extract lastest posts from blogs This post is outdated

Hi,

I'm trying to extract last posts from blogs. I know how to do with single blogs, like this:

{% for article in blogs.mylen-news.articles limit: 5 %}
<li><a href="{{ article.url }}">{{ article.title }}</a></li>
{% endfor %}

But I don't know how I can do it with all my blogs merged.

For example, I've 5 blogs (one for each specific category) and in my homepage I want to extract last 6 news, generally, independant from which blog.
Spefically, if I have category1, category2, category3, category4, category5, and my last 6 articles are published 3 in category1, 1 in category2 and last 2 in category5, I want it to show them arranged in chronological order in my home.

Have only tags in blogs, not categories and have to arrange everthing with this limitation is a bit hard.
My question now is: How I can extract lastest 6 article from all my blogs merged together and order them in chronological order?

Thanks a lot.

i
Replies
Posts:
19
July 04, 2013

someone have any clue? I still cannot merge and order an array of all news in my blogs

Posts:
19
July 05, 2013

do somebody ever did something like this?

Jason Shopify Expert freakdesign.com.au
Posts:
7986
July 05, 2013

I guess you could just grab the last 6 from each blog and do a loop to figure out which 6 are the latest. It will be quite the spectacular loop, but it should be doable. To get your starting date (to work back from) would be just comparing each of the newest posts in each blog. Once you have the latest, add that id to a 'skip' list and loop again. Rinse and repeat.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
19
July 05, 2013

Thanks for the answer, as a programmer that was the solution I was adopting but was a bit "patchy", I was getting last 6 posts from each blog (because on the worst scenario, all my 6 recents posts are of the same blog) and I was trying to extract the date to compare it, sort it and arrange it.
It would generate, on the pessimistic case, a matrix of 6x6 compares to get each artiche out.
I'm still trying to figure out how to use array of items with liquid to use a simple sort algorithm on it and just take first 6 items ordered.

Thanks for our answer and time Jason

Jason Shopify Expert freakdesign.com.au
Posts:
7986
Last edited July 05, 2013

 I was getting last 6 posts from each blog (because on the worst scenario, all my 6 recents posts are of the same blog) and I was trying to extract the date to compare it, sort it and arrange it.

That's pretty well my thought.

Of course, if this was me I'd being doing this through the api instead. The loop and sorting process would be far easier.

[edited: I removed my error caused by brain malfunction]

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
19
July 05, 2013

You're right, I'm not skilled using Ajax API on shopify and this will be a great chance to test it.
Have you some hint or example? I searched on documentation but it only refers to products and not blog elements.

What I should include in my code to use ajax api? How can I get specific blogs last posts from ajax api?
How can I impaginate with ajax when I have my 6 lastest news data array ?
Is I can have some hint I can then work on the code by myself and write an extracting algorithm.

I know I'm too greedy but I will seriously appreciate help
Thanks again
 

Jason Shopify Expert freakdesign.com.au
Posts:
7986
July 05, 2013

 I searched on documentation but it only refers to products and not blog elements.

You're right of course. I was thinking back to my last project when I was pulling in blogs via json, but the feeder was a php app. Still API, but not the AJAX version. This is what happens when you are on the forums and not sleeping.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
19
July 05, 2013

:) thanks for all, I'll try to figure out this problem, at least as a programmer I am good as problem solver too.

Will post here a reply with a solution.

Will be really nice from shopify programmers to include a funcion that grabs all the posts from a single pool, or will be great to have one blog and different categories (with tags).

Have a good nap ;)

 

Jason Shopify Expert freakdesign.com.au
Posts:
7986
July 05, 2013

Have a good nap ;)

Can't. I'm about to write something to do multi-blog sorting. Have to get it out of my head. I'm sure we'll both end up with a similar solution.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Jason Shopify Expert freakdesign.com.au
Posts:
7986
July 05, 2013

Ok, I would just throw all the blog posts into an array with a YYYMMDD at the front of the string. Something like this could get you on your way.

{% assign foo ='20130630|<div class="foo">20130630: Lorem ipsum</div>||20130628|<div class="foo">20130628: Lorem ipsum</div>||20130702|<div class="foo">20130702: Lorem ipsum</div>||20130704|<div>20130704: Lorem ipsum</div>||' | split:'||' %}
{% assign foo = foo | sort %}
{% for f in foo reversed %} 
    {% assign a = f | split:'|' | last %}
  {{ a }}
{% endfor %}

You'd just need to limit as required.

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
19
Last edited July 05, 2013

definitely, I'm in a meeting right now, will code it in a while, thanks for your hel, I really appreciate it

Posts:
19
July 09, 2013

I'm still stucked here, the code you wrote was pretty what I did after we talked here, but what I still don't know is:
How can I create an Array record where push all my articles?
I found nothing about this topic and seems to be a lack of documentation or of Liquid code.

I just want to extract all my articles and push them into one array to sort.

The code after initialized my array will be:

{% for article in blogs.mylen-news.articles limit: 6 %}
	{{ all_blog_articles | push: article }}
{% endfor %}
  
{% for article in blogs.moda-e-lifestyle.articles limit: 6 %}
	{{ all_blog_articles | push: article }}
{% endfor %}
......

Is there any solution for this? how can I create an array and add items to it?

 

Thanks again

Jason Shopify Expert freakdesign.com.au
Posts:
7986
July 09, 2013


Assume this code has errors - it's for example only

 

{% assign foo = '' %}
{% assign bloglist = 'mylen-news,some-other-blog,another-one' | split:',' %}

{% for b in bloglist %}
     {% for article in blogs[b].articles limit: 6 %}
	     {% assign article_date = article.created_at | date: "%Y%m%d" %}
	     {% assign article_content = article.content %}
	     {% assign foo = foo | append:article_date | append:'|' | append: article_content | append:'||' %}
     {% endfor %}
{% endfor %}

{% assign foo_articles = foo | split:'||' %}
{% assign foo_sort = foo_articles | sort %}
{% for f in foo_sort reversed %} 
    {% assign a = f | split:'|' | last %}
  <article>{{ a }}</article>
{% endfor %}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
19
July 10, 2013

Hi, I'm back.
I've used your solution and was almost perfect.
Now I correctly have all my articles, sorted, in an array, there is only one big problem, after that I have an array of "article_content", but I prefer an array of article objects because I need to impaginate them so I need titles, contents, excerpts and URLs to link them.

To achive that I have 2 solutions, one easy and one hard (and I want to figure out the hard one)
1) I can append to my foo string those informations to have something like DATE | TITLE | URL | CONTENT || <-- the double to split single articles as you did.
and extract everything after, but my string will be so heavy because it have 30 articles appended in only one variable
2) I can simply have a DATE | ARTICLE ID || array so I can call a certain article in my second loop and access to all object informations, this is more elegant and useful.

This is the code I've modified to handle solution number 2.
 

{% assign foo = '' %}
{% assign bloglist = 'mylen-news,moda-e-lifestyle,arte-e-cultura,musica,viaggi' | split:',' %}

{% for b in bloglist %}
     {% for singleArticle in blogs[b].articles limit: 6 %}
  <!-- <li> {{singleArticle.title}} </li> -->
  
  		 {% assign article_date = singleArticle.created_at | date: "%Y%m%d" %}
	     <!-- {% assign article_content = singleArticle.content %} -->
  		 {% assign article_id = singleArticle.id %}	
	     {% assign foo = foo | append:article_date | append:'|' | append: article_id | append:'||' %}
  
     {% endfor %}
{% endfor %}
  
  
    {% assign foo_articles = foo | split:'||' %}
  	{% assign foo_sort = foo_articles | sort %}
  	{% assign counter = 0 %}
	  {% for f in foo_sort reversed %} 
  		{% if counter < 6 %}
  			{{f}}
  			{% assign idA = f | split:'|' | last %}
  			{% assign counter = counter | plus: 1 %}
  		{% endif %}
     {% endfor %}

NOTE: The reason I've used a counter is because limiting the "(% for f in foo_sort reversed limit: 6 %)" wasn't working, it applies just before the sort operations so I will only have my first 6 articles sorted and not from all my list.

As you can see, in my {{ f }} I have a couple DATE | ARTICLE ID || elements but now I cannot figure out how to call a certain article with IDs.
I've done kind of what you did in your example with blogs[id] but it isn't working for article[id].

That's my last step to solve this problem.

Thanks for your help, I definitely have to pay you some beers :)

Jason Shopify Expert freakdesign.com.au
Posts:
7986
July 10, 2013

I've used your solution and was almost perfect.

That could be luck. I blasted that out without giving it much though. You probably shouldn't keep my variable names either -- they are only named that way out of laziness so I hope you change them to something meaningful. 

I've done kind of what you did in your example with blogs[id] but it isn't working for article[id].

Rather than making a record of the article id, try it with the index instead. So something more like this:

{% assign foo = '' %}
{% assign bloglist = 'mylen-news,moda-e-lifestyle,arte-e-cultura,musica,viaggi' | split:',' %}

{% for b in bloglist %}
     {% for singleArticle in blogs[b].articles limit: 6 %}
  		 {% assign article_date = singleArticle.created_at | date: "%Y%m%d" %}
  		 {% assign article_index = forloop.index0 %}	
	     {% assign foo = foo | append:article_date | append:',' | append:b | append:',' | append: article_index | append:'|' %}
     {% endfor %}
{% endfor %}
  
{% assign foo_articles = foo | split:'|' %}
{% assign foo_sort = foo_articles | sort %}
{% assign counter = 0 %}
{% for f in foo_sort reversed %} 
	{% if counter < 6 %}
		{% assign idA = f | split:',' %}
		{{ blogs[idA[1]].articles[idA[2]].content }}
	{% endif %}
{% endfor %}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
19
July 10, 2013

Hi, that sounds good, I've tested it right now and I have those results:

  {{idA[0]}}  {{idA[1]}}  {{idA[2]}} = 20130709 moda-e-lifestyle 1
but
{{ blogs[idA[1]].articles[idA[2]].title }} returns me nothing, even {{ blogs[idA[1]].articles[idA[2]].content }}

isn't this the proper way to reach those objects?

Jason Shopify Expert freakdesign.com.au
Posts:
7986
July 11, 2013

isn't this the proper way to reach those objects?

It is.

If that's failing I'd assume Shopify still likes to return everything in a string so the array[idA[2]] will really be array["1234"].

I remember struggling with string -> int conversion ages ago with no luck so I just avoid it now - hence my crazy big string idea in the earlier example. I had thought, since you had been going down this path that we could do it now. 

It doesn't look that way.

In some cases you can do a conditional check (if x = "2") but here, the numbers could be so huge that this would be impractical.

Unless someone comes up with a plan that can return a true integer, you might have come as far as you can. The super bloated (and totally insane) string might have to do.

Talk to Shopify and see if there's a magical hidden way of getting an integer.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
19
July 11, 2013
g
1
upvotes

And I'm back here with good news, I've worked a bit on the previous solution that we skipped to make all perfect and I modified it to achive this awesome result: http://www.mylenstyle.com/pages/list-blog-articles

Here you can see my last 6 articles from all my blogs, sorted by published date, with correct impagination and excerpt over content (I will embed this in my home page).
Most of you will check only last posts on this thread so I'll paste the entire code (even with CSS) that me and Jason were testing to achive this lack in liquid code/shopify features.

<style typee="text/css">
  .home_blog {
    width:45%;
    float:left;
    padding-right: 20px;
    padding-left: 20px;
    margin-bottom: 20px;
  }
 
  .home_blog img {
    height:100px;
    float:left;
    margin-top:20px;
    padding-right: 10px;
  }
</style>


{% assign foo = '' %}
{% assign bloglist = 'mylen-news,moda-e-lifestyle,arte-e-cultura,musica,viaggi' | split:',' %}

{% for b in bloglist %}
     {% for singleArticle in blogs[b].articles limit: 6 %}
  
  		 {% assign article_date = singleArticle.published_at | date: "%Y%m%d" %}
  		 {% assign article_title = singleArticle.title %}
  		 {% assign article_content = singleArticle.excerpt %}
  		 {% assign article_url = singleArticle.url %}
  {% assign foo = foo | append:article_date | append:'||' | append:article_title | append:'||' | append: article_content | append:'||' | append:article_url | append:'|||' %}
  
     {% endfor %}
{% endfor %}
  
  
    {% assign foo_articles = foo | split:'|||' %}
  	{% assign foo_sort = foo_articles | sort %}
  	{% assign counter = 0 %}
	  {% for f in foo_sort reversed %} 
  		{% if counter < 6 %}
  			{% assign article_obj = f | split:'||' %}
<div class="home_blog">
  <h2><a href="{{article_obj[3]}}">{{article_obj[1]}}</a></h2>
  {% assign date_formatted = article_obj[0] | date: "%d/%m/%Y" %}
  <h5>Posted on {{date_formatted}}</h5>
  {{article_obj[2]}} <a href="{{article_obj[3]}}">Read More...</a>
  			{% assign counter = counter | plus: 1 %}
</div>
  		{% endif %}
     {% endfor %}

 

Thanks again for your help Jason, without your pro advices this task probably will still be unsolved.
-FabioQ