Dynamic title Query

Share to Facebook Share to Twitter Share to Google Plus


There is no excuse for modern businesses not to have a blog or some version of one. Constantly updated content is key to your ranking on Google and will provide your customers with more information about your company or product. When Google indexes a page, it needs to know what to show the search user as a title or they won't click on it.

Why use dynamic page titles?

Page titles are VERY important because it's what the search user bases their decision on which link to visit. If every single blog post of yours says "Marketing Company - Blog" then users will be much less likely to view/link to that blog posting than if the page title said, "10 Strategies for Effective Marketing in Lynchburg, Virginia" Your titles need to be specific.

How do I dynamically create a page title?

The first thing you will want to do in this tutorial is copy all of text in your <head> section into a PHP file called "header.php". The file should look something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>My Business Name</title>

If we were to use this as is, every page would have the page title: "My Business Name". We want to dynamically generate this page title so we will replace the text between the "<title>" tags with some PHP code:

if (isset($pageTitle)) {
echo $pageTitle;
} else {
echo "My Business Name";

This will set the default title as "My Business Name" but also allow us to change the page title if we would like. Now go into each one of your pages and replace the <head> section with the following code:

$pageTitle = "Page Title Here";
include('header.php'); ?>

Make sure that each file that you do this to is a PHP file (ex. filename.php). Now all you have to do is change the "Page Title Here" text to the name of that page. If it's the contact page, change the text to: "Contact Us | My Business Name" or whatever you would like.

For blog posts, you can dynamically create the title of the page in our MySql Query like this:

$sql = "SELECT * 
FROM `articles` WHERE id={$articleId} 
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);

$pageTitle = $row['title'];

This will make your web pages more searchable and users more likely to visit your specific article.


Source :

Cody DaltonLead Programmer, Project Manager Has designed websites and developed web applications for multi-million dollar companies including JPMorgan Chase Bank. He is skilled in many programming languages including PHP, Javascript, HTML, CSS, jQuery, MySQL, and more.