For a while back in March I had changed the look of my main blog page so that it showed entries from my Quick Reviews blog interleaved with my main blog entries, in the appropriate chronological order. Frank suggested that I should put together a tutorial on how to do this with Movable Type, so here it is.
We're currently running 7 distinct blogs on sunpig.com, all using the same Movable Type installation. Previously, when we wanted to include content from one blog in another one we would create a new template in blog 1, and then use a server-side include in blog 2 to pull in the content. Here's an example:
Headlines index template in blog 1:
Main index template in blog 2:
This is probably the simplest way of including content from one blog into another. All it requires is the basic Movable Type installation (no plugins needed), and a server that supports server-side includes (SSI).
Although the example above shows how to include content in your sidebar, the technique is exactly the same if you want to put the full entries in the main body of your page. All you have to do is change the template in blog 1 to produce the full entries (or excerpts) instead of just the headlines, and put the #include line in the appropriate position in blog 2.
Unfortunately this method doesn't allow you to interleave entries from both blogs. You will just end up with two blocks of content:
- Blog 2 entry 3 (Friday)
- Blog 2 entry 2 (Wednesday)
- Blog 2 entry 1 (Monday)
- Blog 1 entry 3 (Thursday)
- Blog 1 entry 2 (Tuesday)
- Blog 1 entry 1 (Sunday)
We want to get to a position where the blog entries are merged together in (reverse) chronological order:
- Blog 2 entry 3 (Friday)
- Blog 1 entry 3 (Thursday)
- Blog 2 entry 2 (Wednesday)
- Blog 1 entry 2 (Tuesday)
- Blog 2 entry 1 (Monday)
- Blog 1 entry 1 (Sunday)
To do this we need two things:
- Your MT installation must be running on MySQL, or another SQL-capable database, rather than the default Berkeley File DB. (See the notes on how to updgrade.)
- Brad Choate's MTSQL plugin
The MTSQL plugin does many things, but primarily it gives you much finer control over what entries you want to extract from your blog entries. The basic <MTEntries> allows you to select a list of entries based on category and/or author, to limit the number of entries shown, and to sort them by a small set of criteria (title, date, etc.). MTSQL provides a <MTSQLEntries> tag that allows you to select entries based on pretty much any set of criteria you can imagine, and sort them however you want. You need some knowledge of the SQL database language to put together complex selections, but interleaving two (or more!) blogs is exceedingly simple.
All you have to do is go into your main index template and replace the <MTEntries> tag with an <MTSQLEntries> tag, like so (make sure that this stays all on one line):
<MTSQLEntries> tag for interleaved blog entries:
The query parameter describes the list of entries you want to display. The query above does three things:
- It selects published entries from the blogs with ID numbers 1 and 2: "
select entry_id from mt_entry where entry_blog_id in (1,2) and entry_status=2". To make this work for your own installation, you need to know the ID numbers of the blogs you want to interleave. You can merge as many blogs as you like by expanding the list in brackets. Theentry_statusvalue is there to make sure that you only show entries whose status is set to "published". - It shows these blog entries in reverse chronological order: "
order by entry_created_on desc". - It limits the entries displayed to the first ten in this reverse chronological list: "
limit 0,10". You can change this to show as many entries as you like, e.g. "limit 0,50" would show the most recent 50. If you wanted to skip the first 10 in the list, and show the block of entries from 10 to 20, you would set the clause to "limit 10,20" (like the "offset" parameter in the standard <MTEntries> tag..
The unfiltered parameter must to be set to 1, to stop Movable Type from further restricting your list of entries. Without the parameter, MT would exclude "draft" entries (which we're already doing by saying "entry_status=2"), and entries that don't belong to the current blog, which is what we're trying to avoid in the first place.
Here's a section from the default Movable Type index template, with the <MTEntries> tag replaced by <MTSQLEntries>:
Movable Type code to show interleaved entries from multiple blogs:
That's all you need to produce simple, interleaved entries. But why stop there? With just a little additional effort you can take the next step and give entries from the different blogs their own distinct look. The simple way to do this is by using Kevin Shay's Compare plugin. Just like MTSQL, the Compare plugin is a bit of a Swiss Army knife, but all you really need is the <MTIfEqual> tag it provides.
The following code is another version of the default Movable Type index template. As before, it uses the <MTSQLEntries> tag to retrieve an interleaved list of entries. But inside the <MTSQLEntries> tag we're using <MTIfEqual> to check which blog an entry belongs to, and to format the entries accordingly:
Movable Type code with interleaved blog entries and different visual styles:
Note that the query parameter on the <MTSQLEntries> tag is slightly different. Instead of selecting just a list of entry ids, the SQL code also grabs the blog id for each entry: "select entry_id, entry_blog_id from mt_entry". We need this so that the <MTIfEqual> tags can check this value for every entry:
Using <MTIfEqual> to check the blog id:
The <MTIfEqual> has two parameters, a and b. If the values of these two parameters are equal, then code inside the <MTIfEqual> block will be run. If not, then Movable Type will skip over the block. In this example, the a parameter holds a reference to the blog id for each entry, as represented by "[MTSQLColumn column='2']".
The chunk of template code contains two <MTIfEqual> blocks. The first block processes entries from blog 1, and the second deals with entries from blog 2. In this case, the only difference between the two blocks is that the background colour applied to the entries (khaki or lightsteelblue), but you can customize these blocks in exactly the same way as you would any other Movable Type template.
As thousands of highly individual Movable Type blogs can attest, there's almost no limit to the number of ways you can customize a list of blog entries. Go wild!


Sassy
Hi!
I did the first option and tried to pull the contents from excerpt.html from two other blogs in 2 different subdomains at cooking.houseonahill.net and women.houseonahill.net. Nothing came out. They were supposed to appear on the right column.
You can view it at http://houseonahill.net.
Can you help me out please?
Thanks.