Home > Google > How to add unique titles in php for SEO

How to add unique titles in php for SEO

February 14th, 2009 ogletree

One of the thing I run into the most when evaluating a website for seo is the same title on every page. This can be fixed with a little bit of code. If all the pages are static you just go to each page. If the page is using a header file then you have to make some changes. If they have a lot of static pages with an include file for the header you are still going to have to go and change every page. If you have a lot of data in a database you can just make some changes in the header file. Here is the php code to fix the problem.

Website using header file on a bunch of static pages.

Include File:
<title>
<?php
if($title){echo $title; }else{ echo "Generic Title";}
?>
</title>
 
Static Page
<?php
$title = "Unique Title";
include 'header.inc';
?>

Your still going to have to go to every page and put that on it. This allows you to keep a header file that you can change in one place. If your site is completely dynamic your going to need to add a title field to your database and populate it with Titles. Then use the same if statement on the title tag but echo the title from the db.

Categories: Google Tags: , ,
  1. March 5th, 2009 at 10:18 | #1

    do the same also for keywords

  2. April 8th, 2009 at 04:12 | #2

    If you use SEO friendly URLs, and are lazy like me, you could use these to set the title. This removes the need to specify anything on any page.

    as in:

    $title_holder = explode(“/”, $_SERVER['PHP_SELF']);

    gets the URL i.e. domain.com/my-page-title and explodes it on the first ‘/’

    $title = $title_holder[1];

    sets $title to the bit *after* the forward slash

    $title = str_replace(“-”,” “,$title);

    Replaces hyphens with spaces

    $title = ucwords($title);

    Converts the first letter of each word to capitals so it looks neat

  3. April 16th, 2009 at 10:14 | #3

    thanks , really helpful.
    i searched it a lot and finally got it here.
    proper working one.
    i liked your other posts too.

  4. April 21st, 2009 at 00:42 | #4

    I have just about completed my first php website and am not sure how to add a title tag for each of the pages ? I have used Title tag but the title is not coming up at the top of any of my pages.

    Is there another way title tags should be written when developing a site in php as to opposed from the traditional XHTML sites ?

  5. May 13th, 2009 at 06:43 | #5

    thanks for this great article.
    making unique titles is what i was finding .

  6. May 15th, 2009 at 04:14 | #6

    well i was facing problem and couldn’t really was able to solve it.
    your article has somewhat give me the idea what was the problem i was facing.
    thanks.

  7. May 17th, 2009 at 11:19 | #7

    well, thank you , you don’t know how much this post helped me.
    there maybe other solutions to this one , but your post makes it look easy.

  8. June 12th, 2009 at 15:57 | #8

    Thanks its really good explain nice example given

  9. June 14th, 2009 at 00:28 | #9

    what wrong am i doing , can’t get this code work

  10. June 14th, 2009 at 07:10 | #10

    i was looking for something similar , i hope this would help.

  11. June 17th, 2009 at 04:01 | #11

    The site is calling content from a database, and I am wondering how to have the Meta Title match the topic of the page. Currently every single page has the same title. I want each page to have a unique title. For example, let’s say the following page is about “cats”.
    http://www.example.com/products.php?item=4

  12. June 17th, 2009 at 12:07 | #12

    Instead of

    $title = “Unique Title”;

    do

    $result=mysql_query(“select textfiled from record where user_index=1″);
    $row=mysql_fetch_array($result);
    $title = stripslashes($row['textfiled']);

  13. June 17th, 2009 at 12:09 | #13

    The code in the post is to be on two pages. You have a header file and a your page file. If your not using a header there is no reason to use this code. The post was to help people that are using a header and want unique titles.

  14. June 21st, 2009 at 23:16 | #14

    Thanks for the great and easy tutorial . I need the ability to add a unique title tag and also a description to each page.

  15. June 23rd, 2009 at 13:35 | #15

    Hey, the code is really helpful. Thanks a ton. Easy and a nice way to add unique titles.

  16. June 26th, 2009 at 01:51 | #16

    Helpful code, I was searching for it for a long time now. Thanks!

  17. Lanz Castre
    June 26th, 2009 at 23:27 | #17

    Thanks for postings this code.
    I have saved a copy of it.
    I am not really familiar with Php coding but
    I’ve been keeping necessary codes for Web Development.

  18. June 28th, 2009 at 16:21 | #18

    I am not good in Php Coding but I keep and save necessary codes
    that could help me in the future.
    So thanks for this Code I will keep a copy of this.

  19. Kenny
    July 3rd, 2009 at 07:03 | #19

    Thanks for this bit of code. does this mean I can put that into my site, then when I need to change the title or when I do a new site I dont need to enter a title for each page separately but by using that code every page will be using the title?

  20. July 8th, 2009 at 06:02 | #20

    Hi Ogletree, does it work with template CMS based website as I have seen such issues many times and it is really hectic work to optimize title tag when you don’t find any way to customize them. I guess your solution will work on the CMS template based sites as well.

  21. Joe Barry
    August 31st, 2009 at 14:41 | #21

    Myself, i prefer to just use plain html instead of embedding it into php. It looks messy and sometimes i make mistakes :(

  22. Marci Vue
    November 27th, 2010 at 21:16 | #22

    Blood on your toilet paper? Itching in your anus area? Pain during constipation? All of these are likely signs that you may have hemorrhoids and it’s no laughing matter because although common, they can be annoying and embarrassing.

    Cure Hemorrhoids & Piles Naturally In 48 Hours Without Side-Effects

  23. January 12th, 2011 at 01:42 | #23

    Marci I know your spamming my site but that comment is just way too funny. I took out the links but left it in.

Comments are closed.