Reputation: 2463
I'm looking for a simple straightforward solution for dynamic meta description in a header.php. What I have so far works but I'm not satisfied with the amount of lines, I think this can be done with less code and a nicely structured XML file.
What I have so far:
<?php
$full_name = $_SERVER['PHP_SELF'];
$name_array = explode('/',$full_name);
$count = count($name_array);
$page_name = $name_array[$count-1];
?>
<meta name="description" content="
<?php echo ($page_name=='index.php')?'De Nummer 1 in WebHosting, WordPress Hosting en vps infrastructuur. Professionele support gemakkelijk controlepaneel en vele domein extensies':'';?>
<?php echo ($page_name=='about.php')?'De Nummer 1 in WebHosting, WordPress Hosting en vps infrastructuur. Professionele support gemakkelijk controlepaneel en vele domein extensies':'';?>
<?php echo ($page_name=='webhosting.php')?'web hosting':'';?>
<?php echo ($page_name=='website_builder.php')?'builder':'';?>
<?php echo ($page_name=='wordpress_hosting.php')?'WordPress Hosting':'';?>
<?php echo ($page_name=='wordpress_builder.php')?'WordPress Builder':'';?>
<?php echo ($page_name=='domain_search.php')?'Zoek je Domeinnaam':'';?>
<?php echo ($page_name=='domain_transfer.php')?'Verhuisje domeinnaam':'';?>
<?php echo ($page_name=='contact.php')?'Contacteer ons':'';?>
<?php echo ($page_name=='support.php')?'Support':'';?>"
...
/>
What I am looking for
<?php
$full_name = $_SERVER['PHP_SELF'];
$name_array = explode('/',$full_name);
$count = count($name_array);
$page_name = $name_array[$count-1];
?>
<meta name="description" content="
<?php echo ($page_name==' $page ')?' $content ':'';?>
/>
$page is supposed to be the pagename.php and $content is the meta description for that particular page. There are 20+ pages that are dynamically generated with PHP that all need a particular description, keywords,... I was thinking to put the descriptions and keywords in an XML file and call, depending on page, the right content. How would I do this? I'm quite new to PHP and I spend the last 4 hours trying to solve this... I also tried some tutorials but nothing really suits my needs.
Upvotes: 1
Views: 181
Reputation: 486
There is one simple solution for that.
Step 1
Create meta.ini file and write your file name and meta description like below
index.php="your meta description"
wordpress-host.php="your meta description"
Step 2
<?php
$meta = array();
$metaFile = 'your dir path/meta.ini';
if(file_exists($langFile)){
$meta = parse_ini_file($metaFile);
}
?>
Step 3
<?php
$full_name = $_SERVER['PHP_SELF'];
$name_array = explode('/',$full_name);
$count = count($name_array);
$page_name = $name_array[$count-1];
if(isset($meta[$page_name])){
echo '<meta name="description" content="'.$meta[$page_name].'"/>';
}
?>
Upvotes: 1
Reputation: 1977
What do you need your meta description tag for ?
If it's only for SEO purposes, you won't have to worry : there is no weight (Or so little it doesn't matter) for this parameter for the page ranking.
Google (And other search engines) are capable of retrieving specific data on the webpage, according to the query. So, for many different cases, having a specific metatag won't be useful.
A well-written meta description can be useful when Google isn't able to automatically retrieve content from your page whiwh matches the query : it may be displayed in the search results page. It has to be written in a markting way, in order to make the user want to click the link.
As for the meta keyword, don't waste your time on it : most search engines don't use them (Some of them, like Google, never did)
Upvotes: 0