BellaBuffs: Dynamic Page Titles

I love talking about BellaBuffs, don’t I? Jem loves me because of that.

This tutorial requires that you have a basic understanding of PHP includes (more specifically of using the function to include a header and footer file). It is also best if you are at least a little familiar with BellaBuffs and you should know a bit what the “if…else” function does.

What exactly will you achieve with this tutorial? I am one who likes “clean” page titles. Every page should contains the title of the site preceded (or followed, but I prefer to put it first) by the individual title of the page (for example, “Resources”, “About Us” or “How to create a butterfly in Photoshop”). Well, I will here explain you how to get such titles in BellaBuffs. I’m talking about the <title> tag here.

First, you must prepare the header.php file. If you renamed it, well, then, go ahead with whatever the file is called. The file contains a title tag. Its default content is <?php echo $title; ?> - Powered by BellaBuffs. Now, if you have taken a look into the prefs.php file, you know that $title is not a variable for individual page titles, but the variable inside which is stored your fanlisting’s title. We’ll need to change the content of this title tag.

<?php if (isset($thetitle) && !empty($thetitle)) {
echo $thetitle . " · ";
} else { $thetitle = ""; } ?>

Okay, you might be going “huh?” at this, but I’ll explain. The first line (isset and !empty) verifies that our variable, $thetitle, actually exists in the first place, and that its value is not empty. This way, when we actually display it (the echo function on the next line), we’re not displaying nothing or getting a PHP error.

As I already said, the second line displays the actual title. “middot” is probably my favorite entity to use for that purpose. It is simply a dot standing at mid-line height. Of course, you can change this part to anything you like. You might wonder also why I separated the variable and the entity with a dot. It’s simply to “separate” the two “parts” of the echo statement: the variable and the static part. The period will not be displayed. It acts to say “oh, and echo that too, immediately after”. Don’t forget to place a space before and after whatever separator you use.

You can use any other name beside $thetitle, but avoid using just $title as, like we’ve seen above, the $title variable already exists within the script.

Now we can prepare our files. You will need to edit each file of the script, but it’s only a minor edit. There would be a way to do it only with the header.php file, but it would require a lot of if and elseif, and a lot of checks. I much prefer doing it the other way.

The key is simply to add $thetitle = "Title of the individual page"; inside each file before the header file is included.

In the contact.php file, I personally put it just before the first “if” statement—that is, before any other PHP. The page will be called “Contact” regardless of whether the form was sent or not, and of whether there was an error.

Do not define the variable inside the header.php, footer.php, functions.php or prefs.php files!

In the join.php file, I put it just after the two other variables that are declared at the start:

$show_form = true;
$error_msg = NULL;
$thetitle = "Join";

The “fun” file, though is members.php. This file will do more than just displaying the members, it can also sort them by country. I thought it would be interesting, then, to have the title change for each country page, to reflect the fact that the content is a bit different. This will need a bit more playing around than the other pages.

Before header.php is included (before or after prefs.php is included shouldn’t matter), put the following:

if (isset($_GET['c'])) {
$thetitle = "Members: " . $_GET['c'];
else {$thetitle="Members";}

What is $_GET[‘c’]? If you do not know anything of the $_GET superglobal, I recommend reading information on the W3Schools website or just searching for information on it.

$_GET[‘c’] is used in the script to find the country. If, for example, you go to members.php?s=sortCountry&c=Canada, you will see members from Canada. This means that, in the above lines, we are doing the following steps:

  1. Check if we are viewing a single country.
  2. If yes to 1, then display Members: name of country.
  3. If no to 1, then just display Members.

You can then also change the title within the page, that is when it is shown within the <h1> element. Find these lines:

foreach ($newArray as $country) {
foreach ($countryCount as $key => $cc) {
if ($key == $country) {
echo "

  • $country ($cc members)
  • ";

    Just before the foreach statement is the line echo "<ul>";. Before this line, enter echo "<h1>Members</h1>";. This will show the title on the country list page.

    Beneath the foreach statement is the exit function followed by an else. Just before the foreach function that is inside this else statement, enter echo "<h1>Members: " . $_GET['c'] . "</h1>";. This will show the word “Members” followed by the country on individual country pages. Naturally, you can change it to anything you like, such as Members from $_GET[‘c’].

    And voila, you got yourself nifty titles. I’m starting to feel a bit weird, so maybe the last few paragraphs are utter gibberish. If you have questions or comments, just post a comment and I’ll get back to you if I deem it appropriate.


    1. Jem Said:

      How nifty 🙂 I’ll link this entry on the BellaBuffs page so people can seeee 😀

    2. Hev Said:

      Thanks, I came here from the link on Jem’s site. Just to let you know.

    { RSS feed for comments on this post} · { TrackBack URI }

    Leave a Reply

    Fill in your details below or click an icon to log in: Logo

    You are commenting using your account. Log Out /  Change )

    Google photo

    You are commenting using your Google account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s

    %d bloggers like this: