Published by Mijingo

tips icon image

EE Insider Tips
Sponsored by Mijingo's EE 2 Screencasts

ExpressionEngine mini-howtos created by the EE Insider community.

Display entries from one weblog based on the results of another

User's avatar
  • Posted by JonnyUK
  • June 09, 2009
  • 1 person has found this tip helpful.

How do you display entries from one weblog based on the results of another?

The ideal situation would be to be able to nest our weblog entry tags, similar to the following:

{exp:weblog:entries weblog="first_weblog"}
{title}
{body}

{if count 
== 1}

{exp
:weblog:entries weblog="second_weblog"}
{title}
{body}
{
/exp:weblog:entries}

{
/if}

{
/exp:weblog:entries} 

As you can see, we are trying to pull out entries from second_weblog but only when there are 1 or more entries in first_weblog (by running the code on the first loop of first_weblog). Unfortunately, the EE templating system does not allow you to have nested weblog entries (a.k.a. weblog entries tags within weblog entries tags) - so the above code sample would not work.

However, all is not lost - we can get around this with our trusty {embed} tag!

{exp:weblog:entries weblog="first_weblog"}
{title}
{body}

{if count 
== 1}

{embed
="/template_group/template_containing_second_weblog"}

{
/if}

{
/exp:weblog:entries} 

This way we can achieve the same results by seperating the weblog entries tags into individual templates and telling EE to only embed the template containing the weblog entries tag for second_weblog on loop count 1 of first_weblog (therefore if there are no entries in first_weblog, the template wouldn’t be embedded and the entries from second_weblog would not be shown).

You could easily play with this code and tweak it to display entries from other weblogs based on the output of any field / custom field. Another example:

{exp:weblog:entries weblog="first_weblog"}
{title}
{body}

{if secondary_content 
== "second_weblog"}

{embed
="/template_group/template_containing_second_weblog"}

{if
:elseif secondary_content == "third_weblog"}

{embed
="/template_group/template_containing_third_weblog"}

{
/if}

{
/exp:weblog:entries} 

Add Your Comment?

You must have an EE Insider account to post comments on Insider Tips. It's fast, easy and hassle-free.

Sign up now (or login).

Search EE Insider Tips
Sponsored By