0
votes

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!

3

3 Answers

1
votes

Drupal 7 has a title field for each content type. Why don't you use that?

It shows by default and you can change it for every node.

It is this code in your template file.

<?php print render($title_prefix); ?>
<?php if ($title): ?>
  <h1 class="title" id="page-title">
    <?php print $title; ?>
  </h1>
<?php endif; ?>
<?php print render($title_suffix); ?>

$title_prefix (array): An array containing additional output populated by modules, intended to be displayed in front of the main title tag that appears in the template.

$title: The page title, for use in the actual HTML content.

$title_suffix (array): An array containing additional output populated by modules, intended to be displayed after the main title tag that appears in the template.

See https://api.drupal.org/api/drupal/modules!system!page.tpl.php/7 for all the variables available in your page.tpl template file.

If you want to style one part different from the other you could use an extra field for it.

Or if you need to do something with the value (I wouldn't suggest this!!!).

switch ($title) {
  case "About Us":
    // Do something with the title
    break;
  case "Contact Us":
    // Do something with the title
    break;
  case 2:
    // Do something with the title
    break;
}
0
votes

Here's a simple approach:

Add a new field for the content type

Call it title2

Render the two fields

Use css to inline align them and add styling

0
votes

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. :)