Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to add the Dynamic Meta Title and Meta Description on each page through PHP code?

I have over 20 pages PHP website and would like to add meta title and description; I can do this manually at all without PHP variables, but still I want to know how can I add them unique meta title and description on every page.

I have something like this code:

<title><?php echo $pageTitle; ?></title>
<meta name="description" content="<?php echo $pageDescription; ?>">

But I can only make it unique this on one page, not other pages. So, I have to create new variables for each page, I am confused here, please help me out!.

Julien riera
Julien riera
14,665 Points

I guess the anwser is hardly providable without a bit more code. ($pageTitle & $pageDescription for instance)

My first insight would be to take in consideration the "selected" status of a page.

4 Answers

Julien riera
Julien riera
14,665 Points

Your header.php must echo variables. I guess that misses here. Dont put any value, only echo $pageTitle.

Then, You set your $pageTitle (and description if it depends on the page) value at the top of the page. In your contact.php file it means just before your include.

When you include the header.php, it will call the variables declared above to set variables value in it. Normally.

Sorry, I reply from a cell phone right now.

@Julian Riera, I don't know how to reply you directly, so I am updating my code here.

In index.php file, I have written this code:

<?php

$PageTitle = 'This is my page title';
$PageDescription = 'This is my page description';

?>

and then adding the variables into header.php in title and meta description (using include(includes/header.php)), however the problem is, I can only do this for one page, I don't want to mess up with the single title and description which cause the duplicate content penalty by the google, how can I make it unique to all?

Julien riera
Julien riera
14,665 Points

To be honest, that's what I would have done :

  • create the header.php which contains the echo $pageTitle and $pageDescription php code.
  • create the variable $pageTitle at the top of each page and set the value (e.g "home" - "about" - etc)
  • include the header.php under the statement above. (thus for each page but header.php of course)

I got it, thanks, now it works. One question, what is the benefit here for our website. I mean, we can add manually meta description and title without using php code for each page?

Julien riera
Julien riera
14,665 Points

To me, the purpose is to make students like us understand a better way to update a website as you can affect several pages modifying one piece of code.

That's how I get it.

Thank you now I understood.