Jump to content

Page Titles - help needed


chrismarsden

Recommended Posts

Hi peeps,

 

was wondering if anyone could help, i have decided I want to put page titles in to my website at the moment i have a static title which is the business name and slogan, what I want is business name - slogan - page title (E.G home)

 

my title is set in a header.php file which is then included in all pages.

 

any ideas?  :confused:

Link to comment
Share on other sites

the only thing i can think of is setting variables for each page i.e:

 

conquick.php would have a variable set to "quick guide to connections"

 

i would do this for each page then have the script track what page was opened then out put the title which relates to that page, only problem is i have no idea how i would do this...

Link to comment
Share on other sites

Maybe something as simple as this in your header file or a config file that you include:

 

$title['home'] = 'business name - slogan - home page';
$title['conquick.php'] = 'business name - slogan - page title';
$title['another.php'] = 'business name - slogan - another page title';

if(isset($title[basename($_SERVER["SCRIPT_NAME"]))) {
$page_title = $title[basename($_SERVER["SCRIPT_NAME"]);
} else {
$page_title = $title['home'];
}

 

The just echo $page_title.

 

Or something more elaborate along those lines.

Link to comment
Share on other sites

How do you display your header, is it just a simple include for each page? If so there's no point adding unnecessary complexity to it. You may as well just set a variable before the include then check for that in the template:

 

$title = 'Page Name';
require 'path/to/header.php';

 

In your header:

 

<title>Company Name - Slogan<?php if (!empty($title) echo '- ' . $title; ?></title>

Link to comment
Share on other sites

For something so simple, there are many, many ways to skin a cat. One not necessarily being the most correct, though in sticking with Occam's Razor you may want to choose the simplest. If you want the convenience of only having to keep track of custom code in the header, or knowing that the header is before all other custom code, and your titles are all in one, more easily maintainable place, you could easily use an associative array for this.

 

$page = basename($_SERVER["SCRIPT_NAME"]);
$titles = array(
'conquick.php' => 'Ttitle One',
'thispage.php' => 'Title Foo',
'barpage.php' => 'Title Bar');

<title>Company Name :: Slogan :: <?php echo $titles[$page]; ?></title>

Link to comment
Share on other sites

For something so simple, there are many, many ways to skin a cat. One not necessarily being the most correct, though in sticking with Occam's Razor you may want to choose the simplest. If you want the convenience of only having to keep track of custom code in the header, or knowing that the header is before all other custom code, and your titles are all in one, more easily maintainable place, you could easily use an associative array for this.

 



$page = basename($_SERVER["SCRIPT_NAME"]);
$titles = array(
'conquick.php' => 'Ttitle One',
'thispage.php' => 'Title Foo',
'barpage.php' => 'Title Bar');

<title>Company Name :: Slogan :: <?php echo $titles[$page]; ?></title>

 

Hi peeps, this script worked great... cheers for your help people!!! much appriciated.

Link to comment
Share on other sites

The main problems I see with this are that the titles have to be static, you couldn't have dynamic content (i.e. from a database) and vary the title depending on what the user's viewing. You'll also have to keep modifying it should you ever rename a file, make structural changes, etc. If you had two files with the same name, but one within a sub-directory, you're only going to get whichever's first in the array. Plus there'll be a lot of repetition if you want some pages with the same name.

 

Of course if your website is only small - you never mentioned the size - this won't be such a problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.