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 PDF Print E-mail
Written by Cory   
Wednesday, 04 October 2006

I recently worked on a project in which I needed to display the page's title outside of the bounds of the "mosMainBody" command in my template. Before this project, I never even thought about doing that, much less how to do it. Well, I'm happy to report that not only did I figure out how to do it, but I am going to pass it on to you. Here's how you do it.

Page Title

You may be wondering what I mean by "Page Title". The page title is the title that goes along with the current page being displayed on your site. For instance, the title of the page you are reading now is "How to Display Your Page Title in Your Template". If you look in the title bar of your browser (the bar above the browser menus and toolbars), you will see the entire title which says "Cory Webb's How to Joomla! - How to Display Your Page Title in Your Template". That is simply the site title ("Cory Webb's How to Joomla!") combined with the page title ("How to Display Your Page Title in Your Template").

Step 1: Make the Page Title Dynamic

Joomla! gives you an option of what to display in your title bar. You can either display your site name by itself, or you can display the site name along with the page title, like I have done with this website ("Cory Webb's How to Joomla! - How to Display Your Page Title in Your Template"). By default, the page title is already dynamic, so you will probably not need to change anything, but I am including this step just in case you do.

Step 1A: Access your Global Configuration in the administrator panel.

You can access your global configuration in the administrator panel by clicking Site->Global Configuration in the administrator main menu.

Step 1B: Click the "SEO" tab.

The "SEO" tab will look like this:

Step 1C: Set "Dynamic Page Titles" to "Yes".

Step 1D: Click the "Save" button in the toolbar.

Step 2: Get Your Page Title

At the top of your template's "index.php" file, add the following code to get your page title:

 
<?php
 
  $myPageTitle = "";
  $myPageTitle = $mainframe->_head['title'];
  $myPageTitle = substr($myPageTitle, strlen($mosConfig_sitename) + 3, strlen($myPageTitle) - strlen($mosConfig_sitename) - 3);
 
?>

$myPageTitle is just a variable that I am using to hold the value of my page title throughout my template. I will use it in step 3 to display my page title.

$mainframe is part of the Joomla! API that is available for use in your template code. It contains several parameters, one of which is the current page title (_head['title']), which includes the site name.

The line with "substr..." is used to take the site name out of the page title, so that it only displays the page title. For instance, for this page it would remove "Cory Webb's How to Joomla! - ", leaving only "How to Display Your Page Title in Your Template".

Step 3: Display $myPageTitle in Your Template

The following code can be used to display $myPageTitle in your template. Simply copy and paste this code wherever you need to display your page title.

 
<?php echo $myPageTitle; ?>

That's all there is to it. You now know how to display your page title in your template outside of the bounds of the mosMainBody command. Use it wisely.

As always, if you have any questions about this, please feel free to post them in the comments.

Trackback(0)
Comments (71)add comment
jfh: ...
Not sure what you would need this for... Can you list an example of why you would want to, and maybe a link with a view of it? Just kind of curious, mainly because this technique could really go a long ways with things other than the page title, I think.
1

report abuse
vote down
vote up
October 12, 2006
Votes: +0
That's a good question. You might use it to display the page title next to your logo in the header of the page. Or, you might want to put the page title somewhere in the footer.

For most sites, this technique is probably not necessary. I have only used it one time, in the project that I mentioned at the beginning of this article.

You are correct in stating that this technique can be used for other things. The $mainframe object has other variables that you can access to use in your site in the same way.
2

report abuse
vote down
vote up
October 12, 2006
Votes: +0
Sander: ...
Thanks, was looking for this! smilies/smiley.gif
3

report abuse
vote down
vote up
December 03, 2006
Votes: +0
It works like a charm, but now I've my titles twice. Once where I would like to have it and once from the mosMainBody. Is there a "simple" technique to hide the title from the mosMainBody?
4

report abuse
vote down
vote up
December 18, 2006
Votes: +0
Hello Jeroen,

That's a great question. The way you would hide the title from mosMainBody is in the menu manager. For each page in which you want to hide the title from mosMainBody, you need to modify the menu item that points to that page. There should be a setting in each menu item to show or hide the title. By default, they are set to "Show". You will need to change each one to "Hide".

Let me know if you have any questions.
5

report abuse
vote down
vote up
December 18, 2006
Votes: +0
Hi Cory,

Thank you very much. I was searching for a long time to find your solution.
I put a link to this page: http://forum.joomla.org/index....017.0.html

Joomlaz
6

report abuse
vote down
vote up
February 10, 2007
Votes: +0
Thanks for the info Cory, just apply it at Vespa Community Website.

smilies/grin.gif
7

report abuse
vote down
vote up
February 14, 2007
Votes: +0
You're welcome. I'm glad you found it useful. smilies/smiley.gif
8

report abuse
vote down
vote up
February 14, 2007
Votes: +0
Scott: ...
great work man.

One thing though, on the home page in the menu I have it as "home" this also makes the page title as "home"

Is there anyway I can stop this and make my own page title for the home page?
9

report abuse
vote down
vote up
February 16, 2007
Votes: +0
@Scott...

Thanks for the question. Actually, there is a way to change the title. Log in to your administrator control panel. Click on Menu->mainmenu. In the mainmenu Menu Manager, click on the "Home" menu item. Once you are in the edit screen for that menu item, look to the right side of the screen under "Parameters". There is a parameter named "Page title". Just type in there whatever you want your page title to be.
10

report abuse
vote down
vote up
February 16, 2007
Votes: +1
Av: ...
whatever you have said is working fine.........it displays the page title dynamically.........but my question is how do we extend or enhance this ........i.e., i have number of articles under blog...say for example Bussiness....i want the page title to come as Blog/Bussiness...how do i do this???????please do reply.........
11

report abuse
vote down
vote up
February 21, 2007
Votes: +0
Hi Av,

I do not know of a simple solution to that. I'm sure there is a way to access the blog category name from the template, but I do not know it off the top of my head. If I find how to do it, I will post it here.

Cory
12

report abuse
vote down
vote up
February 21, 2007
Votes: +0
Av: ...
Hi Cory,
i just have another query....how do i hide the url say http://localhost/abc/index.php...&Itemid=48
to http://localhost/Bussiness whose itemid=48....i just want to display the title of the content n not the full details...i.e,category id,section id etc..,let me know if u have a solution for this....thank u
13

report abuse
vote down
vote up
February 22, 2007
Votes: +0
For that, you need a component like OpenSEF in conjunction with the SEF URL's functionality built into Joomla.
14

report abuse
vote down
vote up
February 22, 2007
Votes: +0
Av: ...
Hey Cory,
Thanks again for the information.I guess i need to download that component and install in my site.....do i need to edit it??????
15

report abuse
vote down
vote up
February 22, 2007
Votes: +0
You will probably need to modify the configuration. I haven't used that component in a while, so I do not remember all of the specifics.
16

report abuse
vote down
vote up
February 22, 2007
Votes: +0
Amit D: ...
Hi Cory ,

Thanks a TON you made my day ,
was searching for the right solution and here i am with a satifactory smile on my face
thanks again smilies/grin.gif
17

report abuse
vote down
vote up
March 19, 2007
Votes: +0
Hi Amit,

You're welcome. I'm glad this helped you. smilies/smiley.gif
18

report abuse
vote down
vote up
March 19, 2007
Votes: +0
Fireflight: ...
Sweet Jesus, thank you! I spent a day and a half looking for this bit of code! You've saved my sanity!
19

report abuse
vote down
vote up
March 27, 2007
Votes: +0
Hi Fireflight. I'm glad I was able to save your sanity. smilies/smiley.gif
20

report abuse
vote down
vote up
March 27, 2007
Votes: +0
Yunus: ...
hey, this is great. can we use a similar technique to get the section title?

21

report abuse
vote down
vote up
March 31, 2007
Votes: +0
@Yunus: I'm sure that it is possible, but it might be a little more complicated than this. I'll have to look into it.

Cory
22

report abuse
vote down
vote up
March 31, 2007
Votes: +0
chris_: ... http://netstuff.ro
thanks a lot for sharing...
saved me a lot of trouble smilies/cheesy.gif
23

report abuse
vote down
vote up
April 05, 2007
Votes: +0
Fireflight: ...
I seem to have noticed an odd bug. Occasionally the title won't appear on a page. It seems to happen when you reload, or revisit the same page.

Any ideas?
24

report abuse
vote down
vote up
April 07, 2007
Votes: +0
Hi Fireflight... I've never noticed that bug. I'm not sure what could be causing it.

Cory
25

report abuse
vote down
vote up
April 08, 2007
Votes: +0
I've just noticed it myself in the last few days. It seems to be inconsistent, some pages have the effect, others don't. You can see it in action in the above URL. I did hide the page titles from displaying in the main content area as having the title repeated looked odd to me. Could that be the problem?
26

report abuse
vote down
vote up
April 08, 2007
Votes: +0
vitovito: ...
I have used your code it looks good for my but I would like to show first the page title and then the sitename , how can i do this.
Thanks
27

report abuse
vote down
vote up
April 15, 2007
Votes: +0
I have managed to get the page title before sitename now but It seems that on search engine the web first row title show (title - sitename, sitename title) it looks double entry but inverted. Below you can see some code from my head

-
28

report abuse
vote down
vote up
April 16, 2007
Votes: +0
Hi vitovito,

The site name is in the variable $mosConfig_sitename. Just put that anywhere on your page within php tags, and it will display the site name.

Cory
29

report abuse
vote down
vote up
April 16, 2007
Votes: +0
MAny thanks it works great .
30

report abuse
vote down
vote up
April 20, 2007
Votes: +0
albert: ...
hi cory,

I need to pass the pagetitle to a mailto:
do i need to do all you have mentioned
- editing index.php file
- calling up the string

many thnx in advance, cheerio
31

report abuse
vote down
vote up
April 24, 2007
Votes: +0
Hi Albert,

If the mailto is going to be on every page, then it would make sense to put it in the template index.php file. However, if it is just on one particular content item, then you could just type the title directly into the mailto. It really just depends on how you are intending to use it.

Cory
32

report abuse
vote down
vote up
April 24, 2007
Votes: +0
Tempus Thales: ... http://tempco.org
Thank you, thank you! I tired asking about this on the joomla.org forums, and they barely understood the question, much less could offer a simple solution. You're a lifesaver. smilies/smiley.gif
33

report abuse
vote down
vote up
May 31, 2007
Votes: +0
jo: ...
I use the codings to show the page title but i dont know where to type



Reply me
34

report abuse
vote down
vote up
July 04, 2007
Votes: +0
Ernst: ...
Hi Cory, I use both OpenSef and the extended version of the Joomlapatch (Joomlaatwork) and used your above tutorial to show my pagetitles in a seperate location between h1 tags in a module on top of my content page

Somehow I get only a few letters out to be displayed, instead of the whole text
the page title and sitename. What do I miss here? Can you help me?

Regards, Ernst

35

report abuse
vote down
vote up
July 15, 2007
Votes: +0
Cory: ...
Hi Ernst,

It sounds like your page title is being created differently from how Joomla generates it by default. The only thing I can tell you is to try different things in the code under Step 2 until you get the right value.

I hope that helps.
Cory
36

report abuse
vote down
vote up
July 15, 2007
Votes: +0
Ernst: ...
Thanks Cory for your reply. I will try & error some further :-)

Regards, ernst
37

report abuse
vote down
vote up
July 15, 2007
Votes: +0
Adhe: ...
Hi Cory,

Thanks. I was looking for this smilies/smiley.gif

How about if I have that title in image, how can I put that image title?

Thanks before.
38

report abuse
vote down
vote up
July 16, 2007
Votes: +0
Hi Adhe,

If you have the site's title in an image, the simplest thing to do would be to add an img tag to your template index.php file. The purpose for this tutorial was to demonstrate how to display the title dynamically based on the title of the site set in the Joomla configuration. However, you could just as easily put any image in any place you want in your template, regardless of what the title is in the configuration.

I hope that helps.
Cory
39

report abuse
vote down
vote up
July 16, 2007
Votes: +0
How to write PHP code in Joomla site.How can i shifted my site to joomla site
40

report abuse
vote down
vote up
July 18, 2007
Votes: +0
Hi Hema,

There actually is not a simple answer to your question. I'll try to take each point of your question.

How to write PHP code in Joomla site


There are a couple options for writing PHP into your Joomla site. You could write your own custom component, module, or plugin (mambot) to do whatever you need it to do. Another option is to use a plugin like the Jumi plugin which allows you to write PHP code into your content items.

How can i shifted my site to joomla site


If you already have a website with its own design and content, you would need to install Joomla on your server, convert your current design to a Joomla template, and set up your content within Joomla.

It sounds like you are new to Joomla, so I know those answers probably do not make a lot of sense. I recommend installing Joomla on a test server and familiarizing yourself with it. That is the best way to learn.

Good luck!
Cory
41

report abuse
vote down
vote up
July 18, 2007
Votes: +0
Nirav: ...
My Problem is in some pages(Menu Items) it will take correct page title and in other pages it will take home page title.
42

report abuse
vote down
vote up
July 19, 2007
Votes: +0
great hack, you saved my time, I'm not good in php, so didn't know how to do it before I found this post!
43

report abuse
vote down
vote up
September 20, 2007
Votes: +0
mossaab: ... http://mos3ab.net
thank u soo much. i really sufferred before i found this.
44

report abuse
vote down
vote up
September 28, 2007
Votes: +0
thanks man !this tips help me more: ... http://webhostingsky.com
i like this !good joomla seo tips!
45

report abuse
vote down
vote up
October 11, 2007
Votes: -1
Thank you! This was a great tip!
46

report abuse
vote down
vote up
October 19, 2007
Votes: +0
Matt Hayashida: ...
Is this possible with the title alias instead of the title?
47

report abuse
vote down
vote up
November 09, 2007
Votes: +0
Cory: ...
Hi Matt,

I'm sure it is possible, but off the top of my head I do not know a quick answer for you. It would probably involve checking mosGetParam for option, task, and id to make sure that you are on a content item page, the doing a query against the jos_content table to get the title alias.
48

report abuse
vote down
vote up
November 09, 2007
Votes: +0
Matt Hayashida: ...
Thanks. Any tips or snippits on how I could accomplish this?
49

report abuse
vote down
vote up
November 10, 2007
Votes: +0
free joomla template: ... http://joomlatp.com
thanks!i'll try this! i think it's good for site seo!
50

report abuse
vote down
vote up
November 24, 2007
Votes: +0
OK, how do you change the page title for Joomla 1.5

I drop code title code into my php to fake it for now, but I would like to see our title show up with the - and what ever article our viewers are looking at.

Thank you
Nate
51

report abuse
vote down
vote up
December 03, 2007
Votes: +1
Jennifer: ...
Thanks for sharing this tip, it helped me to create the new template I'm working on! smilies/smiley.gif
52

report abuse
vote down
vote up
December 14, 2007
Votes: +0
Do you think you could write the code to display the alias?
53

report abuse
vote down
vote up
December 18, 2007
Votes: +0
Hi Layne - Do you want the content title alias displayed in your template? I need you to be a little more specific about what exactly you need.
Thanks,
Cory
54

report abuse
vote down
vote up
December 18, 2007
Votes: +0
Henry: ...
Can this be re-written to display the category or section the page is in?
55

report abuse
vote down
vote up
December 18, 2007
Votes: +0
Sorry. Yes i would like the content title alias to be displayed. I am currently using your display for the title but I also need to display the alias.
56

report abuse
vote down
vote up
December 19, 2007
Votes: +0
Hi Layne - I will see if I can figure that one out. There are several variables to consider, but I think it is possible.

Just to give you an idea of what is involved, here are the steps that the code will follow:

1) Use mosGetParam to get the value of "option", "task", and "id" in the URL to make sure that "option=com_content" and "task=view", and to use the id for querying the database.
2) Once we have determined that "option=com_content" and "task=view", we know that we are on a page that is using the content component and showing a content item (as opposed to a category, section, blog, etc.).
3) We use "id" from step 1 and run a query against the table "#__content" (which in most cases is "jos_content") to get the title alias of the current content item.

This is not ideal because it forces us to do another query against the database, which could be a performance issue. If there is a better way to do it, I will try to figure it out.
57

report abuse
vote down
vote up
December 19, 2007
Votes: +0
rodrigo: ...
thanks!!!
58

report abuse
vote down
vote up
December 21, 2007
Votes: +0
Any luck on getting the alias to appear yet?
59

report abuse
vote down
vote up
December 26, 2007
Votes: +0
kdobrev: ...
How can I show a static title ( article title) in Joomla 1.5?
60

report abuse
vote down
vote up
February 21, 2008
Votes: +0
Nikolai: ...
well sounds so great, but how does it work with joomla 1.5????
61

report abuse
vote down
vote up
March 08, 2008
Votes: +0
Andy: ...
Just another shmoe looking for a method to accomplish this in v1.5.1. What I really need it just a way to check what section I'm looking at, then assign an alternate style(s) depending on the current section. Thanks...
62

report abuse
vote down
vote up
March 17, 2008
Votes: +0
gray: ...
looking to accomplish this in v 1.5 also. i've spent hours trying to figure it out with no luck
63

report abuse
vote down
vote up
April 30, 2008
Votes: +0
Jennifer: ...
Hi,

Great hack, thanks! One question - what's the best way to still have the page title only be displayed if the content page's Page Title is set to "Show"? If the Page Title is set to "Hide" I don't want it showing up. I figure that I'll need to comment out the Page Title Display in the content module's display file, but I'm not sure how to do the Hide/Show check from within my template. Any thoughts?

Thanks!
64

report abuse
vote down
vote up
May 04, 2008
Votes: +1
Ihwan: ... http://ihwan.info
What a great tips ! I tried it n working great ! Thanks Cory, I'll bookmark this site smilies/wink.gif
65

report abuse
vote down
vote up
May 09, 2008
Votes: +0
Thank You! I REALLY needed this solution!
66

report abuse
vote down
vote up
May 24, 2008
Votes: +0
nfr: ...
Your solution saved me a lot of work - thanks
67

report abuse
vote down
vote up
May 28, 2008
Votes: +0
Hi I used your very useful hack/coding workaround on my last website, only problem I cant get it to work on Joomla 1.5 presumably because I dont know where theyve hidden the "dynamic page title" option. Have you worked around this change in Joomla 1.5 or does it actually linger under some other option within the back end?

Hopeful of another clever workaround

Kate
68

report abuse
vote down
vote up
June 05, 2008
Votes: +0
Guido: ...
Helllo, nice tutorial. It works so far but my problem is, that I want to display exactly the title variable of an article. When I click on "home" (frontpage), I get the "Home" instead of the title of my first article...

Maybe you can help me. That is the last problem on my page to go public.

Best Regards from New Zealand,
Guido
69

report abuse
vote down
vote up
June 21, 2008
Votes: +0
Hello, I don't find this option in Joomla 1.5.2. How do I create dynamic title in newest Joomla ?
70

report abuse
vote down
vote up
June 27, 2008
Votes: +0
Hi Angelina... you will want to read this article:

http://www.howtojoomla.net/content/view/86/1/
71

report abuse
vote down
vote up
June 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
< Prev   Next >
JoomlaShack

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!