Search code examples
phpdrupal-7

Dynamic title depending on page (DRUPAL 7)


Before I made the switch to Drupal (previously just static HTML), I had a div title in my header that changed depending on what page the user was on using the following code:

ABOUT PAGE

<?php
$title = "ABOUT US</span>";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path);  ?>

The reason this worked, however, was because I was using static HTML... which means I had every page set to use its own Index.PHP, located at (for the About page, for example) public_HTML/about/index.php. Therefore, I could edit each page individually to my liking by editing its respective Index file...

Now, with Drupal.. instead of having so many Index.PHP files like I did before (which was messy IMO...), I want to have every page use the same page.tpl.php (with the exception of the front page, which uses page--front.tpl.php).

Can I somehow modify the above PHP code to display a different title using IF statements depending on what page the user is on?

For example... (I have no idea how to PHP code so this is just to give you experts an idea of what I would want..)

Ifpage=about, $title=About Us; Ifpage=contact, $title=Contact Us;

Could this code all be in the same page.tpl.php?

Thank you so much!


Solution

  • Here was my solution since I am unable to position a Drupal-based title block right next to a non-Drupal based logo DIV..

    I created a div called .headertitle and positioned/styled it how I want.

    My header.php is an PHP include file which is called by each page and the headertitle has a PHP condition..

    <?php if(empty($headertitle)) $headertitle = "ASK REAL QUESTIONS, <br><span class='white'>GET FREE ANSWERS.</span>";
    

    echo $headertitle; ?>

    That way, if no text is declared for the .headertitle, it takes the default form of "ASK REAL QUESTIONS, GET FREE ANSWERS."

    ...Now, I had to figure out a way to actually declare text for the .headertitle depending on what page I was on...

    I did this by using a custom template for each page (node) by using this file.. "page--node--nodeid.tpl.php".

    So, for my ABOUT US page, I created a tpl.php file called "Page--node--7.tpl.php".. for this page, the 7 is the nodeid which is easy to find.

    Then, I added the following PHP code to declare text for the .headertitle specifically on the ABOUT US page which looks like this...

    <?php $headertitle = "ABOUT<span class='white'>.US</span>"; include('includes/header.php'); ?>
    

    DONE AND DONE.

    I'll do that for each page. :)