News Feed E-mail

Get How-To's right in your inbox. Subscribe to the HowToJoomla! news feed e-mail. Just enter your email address here:

Delivered by FeedBurner

Login






Lost Password?
No account yet? Register

Affiliates

Azrul's JomComment
JoomlaShack
iJoomla
The Joomla! Store
Member of W3C Sites dot com
How to Display Your Page Title in Your Template - Joomla! 1.5 PDF Print E-mail
Written by Cory   
Thursday, 05 June 2008

In October 2006, I wrote a how-to for displaying your page title within your Joomla! 1.0 template. Since the release of Joomla! 1.5, I have gotten many requests for an article explaining how to do this in Joomla! 1.5. Here is how you do it...

Page Titles in Joomla! 1.5

If you've already worked with Joomla! 1.5, you know that page titles are handled differently than in version 1.0. In version 1.0, if you enabled dynamic page titles (see this article for an explanation), each page would have an html title of "Site Name - Page Title". For example, the HTML title of this page is "Cory Webb's How to Joomla! - How to Display Your Page Title in Your Template - Joomla! 1.5", where "Cory Webb's How To Joomla!" is the site name, and "How to Display Your Page Title in Your Template - Joomla! 1.5" is the page title.

In Joomla! 1.5, dynamic page titles are used by default, and the site name is not displayed in the html title. For example, if this site were running in Joomla! 1.5, you could look up in the browser's window header and see that the html title just said "How to Display Your Page Title in Your Template - Joomla! 1.5". That makes things a bit easier in terms of getting access to the page title, but it causes another problem; your site name is not part of the html title. There is an SEF Patch that enables you to modify the HTML title, but I will show you a trick for doing this in your template. Think of this as 2 how-to's in 1.

Accessing Your Page Title from Your Template

There are 2 objects in Joomla! 1.5 that are extremely powerful and extremely useful for doing some advanced tricks in your template. The first object is JFactory, which is basically the gateway for accessing just about anything in the Joomla! API, and the second is JDocument, which gives you access to your document's properties. Your document is usually defined as the HTML output from Joomla!, but it could also be a PDF, XML/RSS, or raw output/text. In this case, we are only concerned with the HTML output.

To access your page title, simply add the following code to the top of your template "index.php" file:

 
<?php
 
$mydoc =& JFactory::getDocument();
$mytitle = $mydoc->getTitle();
 
?>

That's it. That's all there is to it. Pretty simple, huh? Now, to display your page title within your template, you simply add the following code anywhere in your template "index.php" file that you wish to display the page title:

 
<?php echo $mytitle; ?>

That's all there is to it. Pretty easy.

Modifying Your HTML Title

The next thing we want to do is modify your html title. We will be using the JDocument object that we just accessed. Using this code, you can set the html title of your page:

 
$conf =& JFactory::getConfig();
$sitename = $conf->getValue('config.sitename');
 
$mydoc->setTitle($mytitle.' - '.$sitename);

Notice that I used JFactory again to get a copy of the Joomla! configuration, which I then used to get the value of the site name (config.sitename). Then, I used "$mydoc" from the earlier step to set the html title.

Note that this code sets the html title to "Page Title - Site Name". If you want to set it to "Site Name - Page Title", you need to change the setTitle parameter to "$sitename.' - '.$mytitle". In reality, you can set the html title to anything you want, but I personally think "Page Title - Site Name" makes the most sense.

Putting it All Together

Using the following code, you are simultaneously getting dynamic access to your page title and setting the value of your html title:

 
<?php
 
$mydoc =& JFactory::getDocument();
$mytitle = $mydoc->getTitle();
 
$conf =& JFactory::getConfig();
$sitename = $conf->getValue('config.sitename');
 
$mydoc->setTitle($mytitle.' - '.$sitename);
 
?>

Questions?

As always, please feel free to post any questions or comments you might have.

Trackback(0)
Comments (48)add comment
Jason Boyette: ... http://www.jkboyette.com
Thanks for this tip - I was just searching around on the forums for something like this. I can't wait to try it out.

One reason I will probably change the title to "Site Name - Page Title" is so that my pages sort together when people look at their browser history.
1

report abuse
vote down
vote up
June 06, 2008
Votes: +0
Hi Jason...good point. I hadn't thought of that. Thanks for commenting. smilies/smiley.gif
2

report abuse
vote down
vote up
June 06, 2008
Votes: +1
Jason Boyette: ... http://www.jkboyette.com
Well, it works great. Just added it to my site, and I've got two other sites to update too. Thanks again.
3

report abuse
vote down
vote up
June 06, 2008
Votes: +1
Conlippert: ... http://www.twolipps.com
Thanks for this fix! I could hardly believe they omitted this in 1.5!
Thank you Thank you!
4

report abuse
vote down
vote up
June 09, 2008
Votes: +0
MeSir: ...
i was looking for hours on the internet , until i found this... thank you ... this is great ... tanks for sharing ... respect...
5

report abuse
vote down
vote up
June 09, 2008
Votes: +0
i had someone make my website, so i'm not sure what that index.php file is. i should have full access to everything, so it that something i can find?? please help, i really want to have page titles on my site!!
6

report abuse
vote down
vote up
June 11, 2008
Votes: +0
Hi Paul... The index.php file is located in your template's folder. Looking at the site you linked, your template folder is located at "templates/bristweb/" on your server.

Adding page titles in your template may not be necessary. This is just a nice trick for those that might need it. However, you might want to modify your site's html title. It really just depends on your own preference.
7

report abuse
vote down
vote up
June 11, 2008
Votes: +0
wow, your response time is incredible smilies/smiley.gif

i'm slow though - where would the templates folder be? under one of the menus along the top (ie: site, menu, content, etc.)?

i know, i just think it's a nice subtle touch that's more professional looking.
8

report abuse
vote down
vote up
June 11, 2008
Votes: +0
Hi Paul, actually, I just looked at your site, and it looks like you are using Joomla 1.0. In that case, you should look at this tutorial, which is the same information geared toward Joomla 1.0.

You can edit your template index.php file in Site->Template Manager->Site Templates. Check the box next to your template, and click the "Edit HTML" button in the top right corner. Be careful, though. You could potentially do some major damage to your site.
9

report abuse
vote down
vote up
June 11, 2008
Votes: +0
thanks for the advice.

i read that article too - problem is i don't have "template manager" or "global config" under my "site" menu bar. weird.......
10

report abuse
vote down
vote up
June 11, 2008
Votes: +0
It sounds like the person who developed your site did not give you "Super Administrator" access to the site. You may need to ask them to give you that access.
11

report abuse
vote down
vote up
June 11, 2008
Votes: +0
haha, bummer. ok, thank you!!
12

report abuse
vote down
vote up
June 11, 2008
Votes: +0
Hi Cory !

Thanks for your work and this tutorial.
Ive integrated this:













...to my index.php but nothing happend !?!

It seems the title wont override by the new title.

How can I fix this?

Greetz

X_Burner
13

report abuse
vote down
vote up
June 20, 2008
Votes: +0










14

report abuse
vote down
vote up
June 20, 2008
Votes: +0
Hi X_Burner... try taking the php tags off and submitting your comment again.
15

report abuse
vote down
vote up
June 20, 2008
Votes: +0
http://screencast.com/t/sytNq5qiPZz

Thanks for your quick response!

X_Burner
16

report abuse
vote down
vote up
June 20, 2008
Votes: +0
Hi X_Burner... I'm not sure if it will make a difference, but try to move the php code above the "!DOCTYPE html" line. Let me know if that works for you.
17

report abuse
vote down
vote up
June 20, 2008
Votes: +0
Ive moved the php block up...but nothing changes smilies/sad.gif

BEfore Ive tested this:


And it works but so I got 2 titles smilies/sad.gif

Is there a other method to override the title ?

Greetz

X_Burner
18

report abuse
vote down
vote up
June 20, 2008
Votes: +0
This line:

jdoc:include type="head"


automatically includes the title, so you should not add your own title tags. I have no idea why this is not working for you.
19

report abuse
vote down
vote up
June 20, 2008
Votes: +0
Exactly what I was looking for. Thanks!
20

report abuse
vote down
vote up
June 20, 2008
Votes: +0
Zainal Ariffin: ... http://www.zainalariffin.com
This guide really works!! Thanks!! Good info.. smilies/grin.gif
21

report abuse
vote down
vote up
June 23, 2008
Votes: +1
Hi Cory.
thanks for your codes. it works somehow for me. i use it on title in index.php file but it changes my language (right to left) and structure of template. i mean every menus and banners are moves to right . the last combined code works and change the title but there is 2 problems

1. it changes the structure of website
2. title will change like this " welcome to front page - Freedom Fighter"

god damn it i am tired of it. this welcome to front page is on my title and my front page. you can see it. please help me. what shall i do? by the way i add code in index file at the top of all lines but my front page editor move the code in the "body"

i have some chat box in my web site you can leave comment there for me or answer it here . it doesn't matter att all.

thank you too much
22

report abuse
vote down
vote up
June 30, 2008
Votes: +0
Hi Ardi,

I'm not sure why that would change the structure of your website. It should not affect the structure.

The "Welcome to the Front Page" text comes from the "Home" menu item. Go to your menu manager and select the main menu. Then, click on the "Home" menu item. Under the parameters, there should be an option to set the page title. Just set it to be blank.

Good luck!
23

report abuse
vote down
vote up
June 30, 2008
Votes: +0
Cory thanks .. it was great help.......
at least i could find that damn name "welcome to front page" and change it. so now title changed and i demand you tell me please :

does it has effect in Google and the title also will change in google?
if it is, then shall i use the codes or no?

because my problem was in google that it showed my web title "welcome to front page"
24

report abuse
vote down
vote up
June 30, 2008
Votes: +0
The effect in Google will not be immediate because Google already has a cached version of your page with the title "Welcome to the front page". But, I still think it is worth it to go ahead and change it.
25

report abuse
vote down
vote up
June 30, 2008
Votes: +0
so it is still changes my web structure. i tried all codes separate and has same effects. so i will wait for few days and if title wouldn't change then we will see what i have to do. just tell me which code is better for me to use?
do you have any codes that i use in my web site to show some media player for music? some good one with nice skins?

thank you too much that help me .. you are very nice... you made me happy and stop my headache smilies/grin.gif
26

report abuse
vote down
vote up
June 30, 2008
Votes: +0
I'm not sure what to tell you about the structure issues. I cannot imagine why this code would change the structure from rtl to ltr.

There are some good media player plugins at www.joomlaworks.gr.
27

report abuse
vote down
vote up
June 30, 2008
Votes: +0
Holy god...
lol... i forgot something else ..please forgive me for my many questions.
do you know how shall i change the small logo of joomla on my address bar? some people has some personal logo or small photos on left side of web side address on address bar. this is my last question.
thanks for media
smilies/grin.gif
28

report abuse
vote down
vote up
June 30, 2008
Votes: +0
In your template file (/templates/rhuk_milkyway/), there is a file called "favicon.ico". Simply replace that file with your own favicon.ico file. You can create a favicon.ico file at favicon.cc.
29

report abuse
vote down
vote up
June 30, 2008
Votes: +0
Hello again Cory

i made my favicon.ico and replace it with the old one . also i added a new code in my index file but still it doesn't work.
this is my code and tell me where shall i add it? also tell me if it is incorrect code.



thanks
30

report abuse
vote down
vote up
July 01, 2008
Votes: +0
Hi Ardi...It's working here. I see a new favicon. You might need to clear your browser cache and reload the page to see it.
31

report abuse
vote down
vote up
July 01, 2008
Votes: +0
well i am trying to clear fire fox cache and i can not find it...!!! so it is still same for me. anyway.

about that website that you told me. i checked it and download something but when i wanna install it on website it shows the message "

* Warning! - Failed to change file permissions
* JFolder::create: Path not in open_basedir paths
* Unable to create destination

what is it ???? this is the first time i see this message.

i wanted to add some comment system ( to leave comment on articles in joomla) but i have no idea what is good. do you know some?
32

report abuse
vote down
vote up
July 01, 2008
Votes: +0
Amy Andrews: ... http://amyandrews.name
Thanks so much for this! It worked great!
33

report abuse
vote down
vote up
July 01, 2008
Votes: +0
hello everybody. cory was busy and didn't answer me. does anybody know why when i wanna install some programs on website it shows the message "

* Warning! - Failed to change file permissions
* JFolder::create: Path not in open_basedir paths
* Unable to create destination

what is it ???? this is the first time i see this message.

and i wanted to add some comment system ( to leave comment on articles in joomla) but i have no idea what is good. do you know some? please someone help me smilies/wink.gif smilies/wink.gif smilies/smiley.gif
34

report abuse
vote down
vote up
July 03, 2008
Votes: +0
Hi Ardi,

I'm not sure what caused the warning you got. What were you trying to install?

I use JomComment on this site. You can get it at http://www.azrul.com. It isn't free, but it only costs around $30 USD.
35

report abuse
vote down
vote up
July 03, 2008
Votes: +0
Nilay: ...
Thanks for the fix. Is there a way to remove 'Home' from the title. After the fix it says 'Home - site title'. Thanks.
36

report abuse
vote down
vote up
July 06, 2008
Votes: +0
Thanks for this Cory. I'm new to Joomla and was getting annoyed by having to repeat the site name in the title field for each article. It makes sense now that I've done it and with some basic understanding of PHP and the Joomla API I guess the possibilities are endless when it comes to adding dynamic content to pages via the template.
37

report abuse
vote down
vote up
July 06, 2008
Votes: +0
Caleb Crosby: ...
This is a great tutorial! Thank you so much!

I have a the Title Manager plugin installed so I am able to concatenate my site name into the page title. Naturally, using the document title in the template will now pull in my entire site name too. Is there a way to pull in just the current article title?

Thanks again! Your site is a tremendous resource!
38

report abuse
vote down
vote up
July 09, 2008
Votes: +0
Caleb Crosby: ...
In thinking about it more, I'm wondering if you can show us how to access the menu titles too? This would add a ton of flexibility in how people use this technique to display a title in their template.

Again, thanks so much!!
39

report abuse
vote down
vote up
July 09, 2008
Votes: +0
hi Cory

my problem didn't solve and still i have this error when install anything as follow :



* Warning! - Failed to change file permissions
* JFolder::create: Path not in open_basedir paths
* Unable to create destination

please tell me what i have to do... i am exhausted of joomla..also my admin server was crazy and installed my safe mode = off ...!!! how can i turn it on. i beg you find some solution for it.
40

report abuse
vote down
vote up
July 17, 2008
Votes: +0
all my permissions are writeable and ok. but i do not know why it happen to me
41

report abuse
vote down
vote up
July 17, 2008
Votes: +0
Hi Ardi,

I'm so sorry to hear that is happening to you. I have no idea why that might be happening. The only thing I can think of is to make sure the directory permissions on your server are set to at least 7-5-7, but you say you've already done that.
42

report abuse
vote down
vote up
July 17, 2008
Votes: +0
Hi cory

another thing is that how shall i change my FTP setting? because i put my web site url in FTP host and it doesn't work (has error) and when i add the name of my server FTP address ( D:WWWPESmyfatherlandiranc.....) it has eroor when i enabale FTP as follow:

JFTP::connect: Could not connect to host "D:WWWPESmyfatherlandirancwww m" on port 21

do you know how to do it? may be if i change this then i can install my components or plugins...
43

report abuse
vote down
vote up
July 21, 2008
Votes: +0
That was incredibly easy. I thought I'd have to go through a long winded process to solve the issue, until I found this. Thank you so much for your help!
44

report abuse
vote down
vote up
August 03, 2008
Votes: +0
Thanks for the call title tip. However, from a SEO standpoint, it is better NOT to call your site title as part of the page title. Your page will rank much higher without it, sometimes 10-20 slots higher!
45

report abuse
vote down
vote up
August 07, 2008
Votes: +0
cchun39: ...
Great tip, thanks. Had a question, is it possible for it to show up:

sitename - category name - article title?
46

report abuse
vote down
vote up
August 17, 2008
Votes: +0
$this->getTitle() should do the trick also...
47

report abuse
vote down
vote up
August 19, 2008
Votes: +0
Paul: ...
I found this extremely useful! THANKS!

I’m using this for meta tags and so far I’ve been able to use this technique for the following:

$mydoc =& JFactory::getDocument();
$description = $mydoc->getDescription();
$generator = $mydoc->getGenerator();
$keywords = $mydoc->getMetaData( 'keywords' );
$author = $mydoc->getMetaData( 'author' );
$mytitle = $mydoc->getTitle();
$conf =& JFactory::getConfig();
$sitename = $conf->getValue('config.sitename');

But I can’t get the “Modified Date” to work.

I’ve tried this:

$mdate = $mydoc->getModifiedDate();
(

But, it simply shows the current date and time.

I’d also like to be able to use the “Created Date” but have found no documentation on getting this at all.

Thanks

Paul
48

report abuse
vote down
vote up
August 27, 2008
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 
Tag it:
Delicious
Digg
Technorati
Next >
Advertisement

About HowToJoomla

HowToJoomla was started in September 2006 by me, Cory Webb, a long-time Mambo/Joomla user with a desire to give back to the community that has given so much to him.

HowToJoomla is not the best, most comprehensive Joomla tips site out there. That's not the goal. The goal is to share some of the knowledge and experience that I have gained through years of using the world's best content managment system. I hope that you will find this site useful and visit often. As I come across new tips and ideas, I will post them here for all to see.

I hope you enjoy using HowToJoomla and find it very useful.

Please take some time to read the Disclaimer.

Thanks for visiting!

About Me

I am a 28-year-old husband and father of a beautiful baby girl named Lucy. I have a degree in Electrical Engineering from the University of Texas at Austin, an MBA from Baylor University, and over 4 years experience working with the Mambo/Joomla CMS.

On February 1, 2008, I started a web design, development, and consulting company specializing in designing and developing custom Joomla-powered websites. The new company is aptly named Cory Webb Media, LLC. The double meaning is purely unintentional, but with a last name like "Webb" it is difficult to escape the obvious puns. If you are looking for professional assistance with your website, please feel free to contact me to discuss your project.

I also run HowToJoomla, CoryWebb.com (my personal website/blog), and JoomlaForm.com (a site dedicated to promoting good design in the Joomla! community). In my spare time (if there is such a thing), I volunteer with the youth ministry at my church.

Copyright © 2006 - Cory Webb Media, LLC - All rights reserved.
Powered by Joomla!