Log in

View Full Version : Creating dynamic and static titles



DarkOcean
05-04-2012, 03:13 PM
Hi,

I'm trying to create some seo-friendly titles for site and need help with how to code the title tag.

For the dynamic part I have received this:
<title><?php if( $post ) { echo $post->post_title; }?></title>

Now I want to add some static text next to this, so it would look like:
Photo gallery >> Worlds leading school of photography.

The 'Photo gallery' text is now generated by the dynamic title tag. I just want to know what the correct way is to add the static text: '>> Worlds leading school of photography.'

Any help would be greatly appreciated!

traq
05-04-2012, 07:45 PM
<title><?php if( $post ) { echo $post->post_title; }?></title>
I just want to know what the correct way is to add the static text: '>> Worlds leading school of photography.'...just add the static text:

<title><?php if( $post ) { echo $post->post_title; }?> >> Worlds leading school of photography</title>
however, it would be more robust like so:
<?php
// start
$title = '<title>';
// check the actual property you're trying to use, not just the object
// also, the way you do it above will still cause an error if $post does not exist
// in the end, this:
// if( $post ){ echo $post->post_title; }
// is no different than:
// echo $post->post_title;
// except that you have _two_ opportunities to run into problems.
if( !empty( $post->post_title ) ){ $title .= $post->post_title; }
// add the static part, and finish
$title .= ' >> Worlds leading school of photography</title>';
// when you're ready...
print $title;

also,
Please use the forum's bbcode tags and indent your code to make it more readable:

for php code............
<?php /* code goes here */ ?>
for html...............
<!-- markup goes here -->.....
for js/css/other.......
code goes here................