Jump to content

I want to use php to generate title tags


animedls

Recommended Posts

Hi all , im new to this forum and have posted this question elsewhere on other forums but have not been able to get an answer .

i have purchased a site www.animedls.com about 1 month ago and i am in the middle of updating the site and doing some seo work on it. I believe that my site runs from a template and it just inserts the .php page into the template (i dont know what template it uses). If you have a look at site , the title tags stay the same for all the pages .

My question is how do i generate title tags for each individual page so that the title tag relates to the actual .php and i do not just have one title tag for all pages in the site ?

I am noob when it comes to php so any help is very much appreciated
Link to comment
Share on other sites

The principal could be like this example, include title-php stuff above the html title tag:

[code]

<?php

switch($_GET['page'])
{
  case 'home':
  $title = "Welcome";
  break;
 
  case 'about':
  $title = "About us";
  break;
 
  // etc etc
}

?>

<!-- html starts -->

<head>
<title><?php echo $title; ?></title>
</head>

[/code]

So for example "index.php?page=about" would generate the title "About us" in this case.

If you query for example mysql for articles and want the article title as html title, run query instead of the switch example and declare article title as $title.
Link to comment
Share on other sites

Hi welcome to phpfreaks.com

I would recommend you to read up on the [url=http://php.net/switch]switch statement[/url] if you dont understand the code. it is basically an if/elseif statement.

But this is what the code is doing.

It is grabing a [b]url variable[/b] called [b]page[/b] (example url: mysite.com/index.php?page=home) you can access this variable by using [b]$_GET['page'][/b]. Now it creates a variable called [b]title[/b] ($title) based on the value of the page variable ($_GET['page']). So if page is set to home, it'll trigger the first case in the switch, which is home and set the title variable to hold the string [b]Welcome[/b]. If its not home it'll check to see if page is set to about. If its it'll set the title variable to hold the string [b]About Us[/b].

Now on this line it echos out the titile variable:
[code]<title><?php echo $title; ?></title>[/code]

Hope that helps. The manual shoudl be able to clear up on how the swtich statement actually works.
Link to comment
Share on other sites

This is also a good thing because search engines will also look at the name of you page and if it sees keywords in this as well that can help. If you wanted to go one better you could do a mod-rewrite. Anyway sorry I have strayed a bit from what you were asking there.

Have you looked at keywords and description, you should still have unique keywords and descriptions on your pages, although some say not.
Link to comment
Share on other sites

woudl anyone be willing to do the title tags code on one page for me so i can see the structure of the code and then i could replicate this on all other pages/for other pages . I will give you cpanel access , just one live example and then i'll be able to replicate for all the other pages i have
Link to comment
Share on other sites

By live example you mena something like this:
[code]<?php
// check that page is set and that its not empty, if it is set and not empty we'll use the page variable
// else we'll set it to a efualt value which is home
$page = isset($_GET['page']) && !empty($_GET['page']) ? $_GET['page'] : 'home';

switch($page)
{
    case 'home':
        $title = "Welcome";
        $content = 'Welcome home dude!';
    break;

    case 'about':
        $title = "About us";
        $content = 'so you wanno know stuff about me!';
    break;

    case 'products':
        $title = 'My products';
        $content = 'Hey the following are my products';
    break;

    default:
        $title = '404 Error';
        $content = "Sorry the page '<i>" . $page . "</i>' you reguested was not found. Please try again";
    break;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title><?php echo $title; ?></title>
</head>
<body>

<h1><?php echo $title; ?></h1>
<p><?php echo $content; ?></p>

<p>
  <hr />
  Pages: <a href="?page=home">Home</a> | <a href="?page=about">About Us</a> |
  <a href="?page=products">Products</a> | <a href="?page=duffPage">A duff page</a>
</p>

</body>
</html>[/code]
Link to comment
Share on other sites

If your e-commerce store is running off a database you may try something like

<? include("config.php"); ?>
<title><?
// Get 1 Category Title Below
$result = mysql_query("SELECT *
    FROM womensproducts
        WHERE CategoryID='$cat' limit 1") or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
include("title.php");
}
mysql_free_result($result);
?>

Which my title.php just includes:  <?=$row["Category"]; ?>

You can change this variable to whatever your wanting to change on each page.

Hope this helps.
Link to comment
Share on other sites

[quote author=animedls link=topic=109704.msg443099#msg443099 date=1159454831]
i think i unedrstand but i tkae it , i would have tochaneg all my urls/links on the site to the format "?page=blahblah"

I only include this code on the index page right ?
[/quote]
No. Just chnage [b]page[/b] in the $_GET var to the name of the variable that holds the page that is being reguested.

What is the format of the urls?
Link to comment
Share on other sites

This will be a variant:
[code]
<?php

$is_url = $_SERVER['REQUEST_URI'];
$is_piece = explode(",", $is_url);
$title = urldecode($is_piece[1]);

?>
[/code]

On your first link example the title then becomes "complete anime episodes"
On the second link it is "complete cowboy bebop episodes"
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.