Two columns layout for nodes

PLEASE NOTE: The php snippets are user submitted and it is impossible to check every one, so use at your own risk. When using an alternate database to the default MySQL, some database queries may not work.

I wanted to setup a two columns layout for my frontpage. I don't see any snippet for doing this the way I wanted so here is my solution.

You must use the phptemplate engine for this to work.

1) Make your themefolder/node.tpl.php look like this:

<?php
   
global $node_count;
    global
$node_incr;
    global
$node_cols;

    if (isset(
$node_incr) and $node_incr>-1) {
       
$node_incr++;
    } else {
       
$node_incr = 0;
       
$node_cols = array(0 => "", 1 => "");
    }
  
// dispatch odd index nodes to the left and even index ones to the right
   
$side = ($node_incr/2 == round($node_incr/2)) ? 0 : 1;

// or sends the first nodes in the left column up to $node_count/2
       
$side = ($node_incr<$node_count/2) ? 0 : 1;

       
$buffer = " ... html code ... ";

       
$node_cols[$side] .= $buffer;

    if (
$node_count>1) {
        if (
$node_incr == $node_count-1) {
           
$node_incr = -1;
       
?>


// If it is the last node, the content is printed out
<table>
<tr>
<td style="width: 50%; vertical-align: top;">
<?php  print $node_cols[0];?>
</td>
<td style="width: 50%; vertical-align: top;">
<?php  print $node_cols[1];?>
</td>
</tr>
</table>

<?php
}
} else {
// If $node_count == 1, there's no need to draw a second column
echo $node_cols[0];
$node_incr = -1;
}

2) Add the following to modules/node.module:

global $node_count;
    $node_count = db_num_rows($result);

after
function node_page_default() {
  $result = pager_query(db_rewrite_sql('SELECT n.nid, n.sticky, n.created FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC'), variable_get('default_nodes_main', 10));

  if (db_num_rows($result)) {

3) For taxonomy nodes, add the same to modules/taxonomy.module after:

function taxonomy_render_nodes($result) {
  if (db_num_rows($result) > 0) {

no need for patching the core.

dan90 - May 17, 2006 - 07:15

for my drupal 4.6 site at http://undergrowth.org, I managed to do this by simply wrapping each teaser node in a floated div and avoided tables to boot. I also had to CSS style them to overflow: hide to avoid complications with lumpy content styles. Actually, searching the site, i notice I'm not the first one to think of this: http://drupal.org/node/27441.

 
 

Drupal is a registered trademark of Dries Buytaert.