Jump to content

How Would I Dynamically Change Meta Tag Information?


Recommended Posts

Hi Everyone,

 

How would I dynamically change meta tag information on a per page basis?

 

If a visit is on my "about" or "inventory" page. In the title and meta tag I would want that user to see "about us" or "inventory". I've written some below sample code but it's not working. Not sure what I'm doing.

 

For example, this page should be echoing out "Title Test Page" but it's not. What am I doing wrong here?

 

Thanks!

 

http://whatsmyowncar...3/titletest.php

 

 

<?php
$title;
switch($_SERVER['PHP_SELF'])
{
case "/index.php":
$title = 'Home';
break;
case "/titletest.php":
$title = 'Title Test Page';
break;

}

print '<title>'.$title.'</title>';
?>

Edited by scm22ri

I'm guessing you have a shared "header.php" type file you include across each page? Just define a default config array within your header file, and allow each page to override it:

 

header.php

$defaultConfig = array(
    'title' => 'Your Site',
    'description' => 'Your site description',
    'keywords' => 'your, site, keywords'
);

$config = array_merge($defaultConfig, isset($config) ? $config : array());

 

about.php

$config = array(
    'title' => 'About page'
    // Default description and keywords will be used as we omitted them here
);

include 'header.php';

 

Keeps it nice and simple, and avoids a huge switch statement.

Thanks Adam,

 

I went about this in a slightly different manner. What do you think about my answer? I did in fact use a switch statement. I requested the url as a unique identifier of each page. According to the url it's going to return a specific statement.

 

 

<?php

$title;
$url = $_SERVER['REQUEST_URI'];
switch($url){

case "/class-work/sign3/valchecklogin.php";
$title = 'Home';
break;

case "/class-work/sign3/heythere.php";
$title = 'Profile Page';
break;

case "/class-work/sign3/display-inventory.php";
$title = 'Inventory Page';
break;

case "/class-work/sign3/mygoal.php";
$title = 'My Goal';
break;

case "/class-work/sign3/add-car.php";
$title = 'Add Car';
break;

case "/class-work/sign3/add-dealership.php";
$title = 'Add Cardealership';
break;

case "/class-work/sign3/edit-car.php";
$title = 'Edit Cars';
break;

case "/class-work/sign3/cal.php";
$title = 'Calculator';
break;

// the results page poses a issue. Not quite sure how to fix this?
case "/class-work/sign3/cal-results.php?";
$title = 'Calculator Results';
break;

case "/class-work/sign3/forgot2.php";
$title = 'Forgot Password';
break;

case "/class-work/sign3/change-password.php";
$title = 'Change Password';
break;

case "/class-work/sign3/register.php";
$title = 'Register';
break;

case "/class-work/sign3/contact.php";
$title = 'Contact Us';
break;

case "/class-work/sign3/logout.php";
$title = 'Logout';
break;

}

?>

the results page poses a issue. Not quite sure how to fix this?

 

Well to be honest, those are the kind of problems you'll face splitting out the logic of what title to display, from the logic that actually determines what title should be displayed. If that makes sense?

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.