Community & Support

HOWTO: Create different header/footer(/ any content) per address

Note: I am not certain whether this is the right place to post it, but I guess if not someone will correct my mistake...
And I hope this will interest somebody

Goal:
The site should look different according to the used address. User that uses "xxx.example.com" should see different things then a user that uses "yyy.example.com".
The site administrator should be able to make the change via the web interface - no need for coding or shell/ftp access

Tools:
CCK + Node Reference field
NodeAsBlock
PHPTemplate engine
SWF Tools /Flash Node

Snippests:
1. Verify that the listed modules/tools are installed properly.
2. Create the following content type (if someone knows how to export/import content types, I can provide the needed content type):
- Name: "Node By Host"
- Type: "node_by_host"
- Body field label: remove
- Check "Enable blocks from this node type"
- Fields:
- default_node - "Default Node" - Node Reference
- host1 - "Node 1" - Text
- node1 - "Node 1" - Node Reference
- host2 - "Node 2" - Text
- node2 - "Node 2" - Node Reference
3. Create a node-node_by_host.tpl.php in your theme directory, which includes the following code:

    <DIV class="block_with_borders">

        <?php
           
// Get the host used by the user browser
           
$host = $_SERVER['HTTP_HOST'];

           
// using the default node
           
$selected_nid = $node->field_default_node[0]['nid'];

           
// Go over the defined hosts and select the first one that fits to our site
           
$index = 1;
           
$fieldname = 'field_host' . $index;

            while (isset(
$node->$fieldname)) {
               
$tmp = $node->$fieldname;
               
$site = $tmp[0]['value'];

                if (
$site) {
                   
$pattern = preg_quote("$site", '/');
                   
$pattern = '/^' . $pattern . '/';

                    if (
preg_match($pattern, $host)) {
                       
$fieldname = 'field_node' . $index;
                       
$tmp = $node->$fieldname;
                       
$selected_nid = $tmp[0]['nid'];
                        break;
                    }
                }
               
$index++;
               
$fieldname = 'field_host' . $index;
            }

       
?>

        <div <?php {
          echo
'id="block-nodeasblock-' . $selected_nid.'"';
          echo
' class="block block-nodeasblock"';
        }
?>
>

            <div class="content">
                <!-- Show the node which was selected according to the used address -->
                <?php echo node_show(node_load($selected_nid), NULL); ?>
            </div>
        </div>

    </div>

Inspiration: taken from Referer Theme module, which uses the host to change to a $custom_theme.

Steps:
1. Create the different content you want to display for every host, and a default content. In case you want to show a clean flash banner, for example, create a flash node content per site, and then create additional page content per flash, which will only contain flash node filter "[flashnode|nid=xxx]".
2. Create a node-by-host content, fill the default node, and for every wanted host, a relevant node. Check "create block" for this content, and point it to the wanted region.
Now, when a new site is added, you just need to add the content and update the node-by-host content.

nobody click here