Hi guys!
I need help with meta tags on my website!
Look on all intire website i have more then 20 pages, and im using only one head.php and footer.php files.

So i want to have a meta tags for each page, without dupicating the head.php, and i'm really struggling. is there a way to do this without duplicating the head.php

I hope get some repply from you guys!

Member Avatar for diafol

Sure, there are a hundred different ways to do it.
You could have a config_page.php file:

switch($page)
{
    case 'home':
        $metaDesription= "blah blah";
        $metaTitle= "kdKJVD";
        break;
case 'about':
        $metaDesription= "blah blah2";
        $metaTitle= ";llwjvb";
        break;
case 'contact':
        $metaDesription= "blah blah3";
        $metaTitle= "lskbvlkv";
        break;
}

So you could do this:

$page = ... //get this from your url dynamically or hardcode it.
require "includes/config_page.php";
require "header.php";
//rest of page if in plain html - or require the page itself if in a pages directory
require "footer.php";

Your header could look like this...

<title><?=$metaTitle;?></title>
<meta name="description" content="<?=metaDescription?>" />

(etc)...

Else you could just hard code the variables into the top of each page individually.

$metaTitle = 'skvblwkv';
$metaDescription = 'dklWKJDKJ';
require "header.php";
...

However, we're fast running into templating territory. If you have a lot of reusable content, you may want to start thinking about a template engine, like Smarty, Twig or RainTPL or a hundred others. Similarly, if you have a lot of processing involved with each page, you could start looking at an MVC structure or even a PHP framework, but these may be overkill - depends on the complexity of your site.

commented: good +1

Thanks Diafol!
Really i use Twig for all my rojects, but this project i'm just doing maintance, the applicatiom started with different guys, and they used some cms to build everything, so is just native php there and html.
so thanks i'll use this way with switch page, i think is good solution i have for now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.