...

View Full Version : PHP Include w/ Custom Meta Tags?



phpnewbie99
09-06-2007, 01:05 AM
Hi. I have a template that I use that calls a common header to all pages through a PHP include.

The problem is that I'd like to add custom meta tags individually to each page... but the template is calling the common header...

Each page will need unique meta tags so I need some sort of hack that lets me input the meta tags directly into the HTML while still pulling the common header.

Is this possible? Anyone have any tips?

thanks in advance.

rafiki
09-06-2007, 01:08 AM
pull them out of a DB depending on which page it is?

Inigoesdr
09-06-2007, 01:15 AM
If it's only a few pages use something like this:

switch(basename($_SERVER['SCRIPT_NAME']))
{
case 'test.php':
echo '<meta...>';
break;

case 'contact.php':
echo '<other meta..>';
break;

default:
echo '<default meta...>';
break;
}

phpnewbie99
09-06-2007, 01:37 AM
thanks for the responses... you know i figured it out though. It's not the cleanest way to do it i'm sure but it works...

If you're interested here's what I did.

In the header include file I put:

<meta name="description" content="<? if (isset($metadescription)) { echo $metadescription; } ?>">

Then on the individual page I put this immediately before I called the include:

<?php
$subtitle = "Page Title Here";
$metadescription = "Description Here";
$metakeywords = "Keywords Here";
require '../inc/header.inc.php'; ?>


When I load the page and view source it all comes out as clean HTML with the meta tags properly coded. So the search engines should be fine w/ it.

The subtitle is for the page title so each page can have a unique page title. The code in the include file is the same just w/ subtitle:

<title><? if (isset($subtitle)) { echo $subtitle; } ?></title>



As i said not the cleanest or easiest way i'm sure but it works. Hope someone else can use this.

By the way anyone have any thoughts on how to better this?

rafiki
09-06-2007, 01:39 AM
INIGoesDR (lol)
his idea is probably the better solution.
I'd probably go with that.

aedrin
09-06-2007, 05:20 PM
My solution is a little more complex, but in the end a lot cleaner and easier.

I have a static class called UI that I create per site (it has some resuable components, so it can inherit a base class if need be). This class has several methods to add things like scripts/css/etc. But these can be called from specific scripts to add to the common header. It could be changed to set/modify specific meta tags.

For example;

home.php


UI::addStylesheet('home');


header.php


// ... some basic html here ...

foreach (UI::getStylesheets() as $css) {
echo '<link href="'.$css.'.css" rel="stylesheet" type="text/css" />';
}

// ... more basic html ...


This only works if your pages are executed before the header code. If you properly seperate model/views then you can add the stylesheet in the model, but that would be defeating the purpose of seperating business and presentation logic...

syosoft
09-09-2007, 02:19 PM
If you're trying to create a small, easy to template based site, look into buffers and modrewrite...it's amazing what 50 lines of code can do.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum