Community Documentation

Two columns layout for nodes

Last updated May 13, 2009. Created by 120@drupalfranc... on April 15, 2006.
Edited by bekasu, pwolanin, cel4145. Log in to edit this page.

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) {

Alternative to modifying Core Code

For drupal 4.6 site at http://undergrowth.org, you can do this by simply wrapping each teaser node in a floated div and avoided tables to boot. You will also need to change CSS style them to overflow: hide to avoid complications with lumpy content styles. Actually, searching the site, you'lli notice others that have discovered this: http://drupal.org/node/27441.

About this page

Drupal version
Drupal 4.7.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here