Reputation: 350
I have a single header called on different pages through PHP. Similarly the meta tags have also been called a single time in header. But in order to be detected for SEO i have to include separate meta tags for each page. I have tried to make it work in the following way on the header.php of my site:
header.php
<head>
<?php
$pgKeywords="lorem ipsum dolor sit amet";
$pgDesc="lorem ipsum dolor sit amet";
?>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="keywords" content="<?php echo $pgKeywords ?>">
<meta name="description" content="<?php echo $pgDesc ?>">
</head>
i included the 'header.php' on all other pages (say contact.php, about.php) and called the following only (not the meta tags):
about.php
<?php
$pgKeywords="lorem ipsum dolor sit amet lorem ipsum dolor sit amet";
$pgDesc="lorem ipsum dolor sit amet lorem ipsum dolor sit amet";
include 'header.php';
?>
<div class="cntner_24">
<a href="contact.php" class="cntct" title="Contact us.">
<img src="img/button.png" width="18" height="109" class="center_align">
<img src="img/hover_button.png" width="22" height="21" class="center_align">
</a>
<a href="about.php" class="abt" title="Get to know us better.">
<img src="img/button.png" width="14" height="78" class="center_align">
<img src="img/hover_button.png" width="22" height="21" class="center_align">
</a>
</div>
what i want to know is is this the correct way to do it or is there any other correct way to go about so that these meta tags get detected through the SEO ?
Solution (Final Edit):
As the SEO wasn't able to detect tags from different pages (although the solution given by Mr. Rajesh is correct but they wanted all the meta tags on a single page) so i decided to apply a different approach and implemented it all on the 'header.php' page only (explained below).
By prinitng '$_segments' you'll be able to know which segment your required directory (like index.php, contact.php, about.php etc.) is on, say mine was on 3rd place so i put [3] inside if($segments[3]=='index.php')
<head>
<?php
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $_SERVER['REQUEST_URI_PATH']);
print_r($segments);
if($segments[3]=='index.php')
{
?>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="keywords" content=" lorem ipsum dolor sit amet ">
<meta name="description" content=" lorem ipsum dolor sit amet ">
<?php
}
elseif($segments[3]=='about.php')
{
?>
<meta name="keywords" content=" some different meta tag keywords ">
<meta name="description" content=" some different meta tag description ">
<?php
}
?>
</head>
So, in this way you can have separae meta tags for separate pages, by just putting the page name through the if-else conditioning.
Upvotes: 1
Views: 14251
Reputation: 796
Code Corrected based on your requirement
meta.php
<?php
function meta($pgKeywords,$pgDesc)
{?>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="keywords" content="<?php echo $pgKeywords ?>">
<meta name="description" content="<?php echo $pgDesc ?>"><?php
}?>
contact.php
<?php
include('meta.php');
$pgKeywords="Contact us ";
$pgDesc="lorem ipsum dolor sit amet";
meta($pgKeywords,$pgDesc);
?>
about.php
<?php
include('meta.php');
$pgKeywords="About us ";
$pgDesc="lorem ipsum dolor sit amet";
meta($pgKeywords,$pgDesc);
?>
Upvotes: 2
Reputation: 1413
Define keyword and description on page1.php, page2.php etc before including header.php. So your page1.php will be like
$pgKeywords="lorem ipsum dolor sit amet lorem ipsum dolor sit amet";
$pgDesc="lorem ipsum dolor sit amet lorem ipsum dolor sit amet";
include "header.php";
where header.php will be like
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="keywords" content="<?php echo $pgKeywords ?>">
<meta name="description" content="<?php echo $pgDesc ?>">
Upvotes: 0