MovableBlog: Links Divided Over 2 Columns
December 4, 2002
On the Vancouver Webloggers links page, the links are alphabetized and divided into a two columns. But how?!
With PHP of course.
Place in your index template somewhere this code. I have a whole blog in MT dedicated to links, and in the case of Vancouver Webloggers, use the category "Other" for bloggers other than the authors of the site.
$links = array();
<MTEntries category="Other" sort_order="ascend" sort_by="title">
$links[] = "<$MTEntryBody encode_php="qq"$>";
</MTEntries>
$numlinks= count($links);
?>
Then, where you want the links to appear in two columns, insert this code:
<tr>
<td>
<?php
$half = $numlinks/2;
$half = (int) $half;
for ($i = 0;$i < $half; $i++)
{
print $links[$i]."<br />\n";
}
?></td>
<td><?php
for ($y = $half; $y < $numlinks;$y++)
{
print $links[$y]."<br />\n";
}
?>
</td>
</tr>
</table>
Note that for the title of each link's entry, I take the blog name and capitalize the first letter. Also, I follow the practice of not using "A" or "The" as the first word in the title. So "The Blog That Never Was" is inserted in the the title as "Blog That Never Was, The" and "a blog through time" becomes "Blog through time, A".
Comments
Another way to do this, which would be slightly easier on php, would be:
<? $i=0;$j=0;?>
<MTEntries category="Other" sort_order="ascend" sort_by="title"><? $i++;?></MTEntries>
<? $cut_at = (int)($i/2);?>
<MTEntries category="Other" sort_order="ascend" sort_by="title">
<?
if(++$j == $cut_at)
echo '</td><td>';
?>
<$MTEntryBody$><br />
</MTEntries>
Posted by: Bill Zeller | January 2, 2003 02:42 PM