How to Display Your Page Title in Your Template

Written by Cory | October 04, 2006 | Add Comment (0)

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 (81)Add Comment

0
...
written by jfh, October 12, 2006
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.
0
...
written by Cory, October 12, 2006
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.
0
...
written by Sander, December 03, 2006
Thanks, was looking for this! smilies/smiley.gif
0
...
written by Jeroen, December 18, 2006
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?
62
...
written by Cory, December 18, 2006
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.
0
...
written by Joomlaz, February 10, 2007
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
0
...
written by Miftahx, February 15, 2007
Thanks for the info Cory, just apply it at Vespa Community Website.

smilies/grin.gif
62
...
written by Cory, February 15, 2007
You're welcome. I'm glad you found it useful. smilies/smiley.gif
0
...
written by Scott, February 16, 2007
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?
62
...
written by Cory, February 16, 2007
@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.
0
...
written by Av, February 21, 2007
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.........
62
...
written by Cory, February 21, 2007
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
0
...
written by Av, February 23, 2007
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
62
...
written by Cory, February 23, 2007
For that, you need a component like OpenSEF in conjunction with the SEF URL's functionality built into Joomla.
0
...
written by Av, February 23, 2007
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??????
62
...
written by Cory, February 23, 2007
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.
0
...
written by Amit D, March 19, 2007
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
62
...
written by Cory, March 19, 2007
Hi Amit,

You're welcome. I'm glad this helped you. smilies/smiley.gif
0
...
written by Fireflight, March 27, 2007
Sweet Jesus, thank you! I spent a day and a half looking for this bit of code! You've saved my sanity!
62
...
written by Cory, March 27, 2007
Hi Fireflight. I'm glad I was able to save your sanity. smilies/smiley.gif
0
...
written by Yunus, March 31, 2007
hey, this is great. can we use a similar technique to get the section title?

62
...
written by Cory, March 31, 2007
@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
0
...
written by chris_, April 05, 2007
thanks a lot for sharing...
saved me a lot of trouble smilies/cheesy.gif
0
...
written by Fireflight, April 08, 2007
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?
62
...
written by Cory, April 08, 2007
Hi Fireflight... I've never noticed that bug. I'm not sure what could be causing it.

Cory
0
...
written by Fireflight, April 09, 2007
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?
0
...
written by vitovito, April 16, 2007
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
0
...
written by vitovito, April 16, 2007
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

-
62
...
written by Cory, April 16, 2007
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
0
...
written by vitovito, April 20, 2007
MAny thanks it works great .
0
...
written by albert, April 25, 2007
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
62
...
written by Cory, April 25, 2007
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
0
...
written by Tempus Thales, May 31, 2007
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
0
...
written by jo, July 04, 2007
I use the codings to show the page title but i dont know where to type



Reply me
0
...
written by Ernst, July 15, 2007
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

62
...
written by Cory, July 15, 2007
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
0
...
written by Ernst, July 15, 2007
Thanks Cory for your reply. I will try & error some further :-)

Regards, ernst
0
...
written by Adhe, July 16, 2007
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.
62
...
written by Cory, July 16, 2007
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
0
...
written by Hema, July 18, 2007
How to write PHP code in Joomla site.How can i shifted my site to joomla site
62
...
written by Cory, July 18, 2007
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
0
...
written by Nirav, July 19, 2007
My Problem is in some pages(Menu Items) it will take correct page title and in other pages it will take home page title.
0
...
written by Zaur, September 20, 2007
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!
0
...
written by mossaab, September 28, 2007
thank u soo much. i really sufferred before i found this.
0
...
written by thanks man !this tips help me more, October 11, 2007
i like this !good joomla seo tips!
0
...
written by paul, October 19, 2007
Thank you! This was a great tip!
0
...
written by Matt Hayashida, November 10, 2007
Is this possible with the title alias instead of the title?
62
...
written by Cory, November 10, 2007
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.
0
...
written by Matt Hayashida, November 10, 2007
Thanks. Any tips or snippits on how I could accomplish this?
0
...
written by free joomla template, November 24, 2007
thanks!i'll try this! i think it's good for site seo!
0
...
written by Nate, December 03, 2007
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
0
...
written by Jennifer, December 14, 2007
Thanks for sharing this tip, it helped me to create the new template I'm working on! smilies/smiley.gif
0
...
written by Layne, December 18, 2007
Do you think you could write the code to display the alias?
62
...
written by Cory, December 18, 2007
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
0
...
written by Henry, December 19, 2007
Can this be re-written to display the category or section the page is in?
0
...
written by Layne, December 19, 2007
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.
62
...
written by Cory, December 19, 2007
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 "jos_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.
0
...
written by rodrigo, December 21, 2007
thanks!!!
0
...
written by Layne, December 26, 2007
Any luck on getting the alias to appear yet?
0
...
written by kdobrev, February 21, 2008
How can I show a static title ( article title) in Joomla 1.5?
0
...
written by Nikolai, March 08, 2008
well sounds so great, but how does it work with joomla 1.5????
0
...
written by Andy, March 17, 2008
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...
0
...
written by gray, April 30, 2008
looking to accomplish this in v 1.5 also. i've spent hours trying to figure it out with no luck
0
...
written by Jennifer, May 04, 2008
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!
0
...
written by Ihwan, May 10, 2008
What a great tips ! I tried it n working great ! Thanks Cory, I'll bookmark this site smilies/wink.gif
0
...
written by shawn, May 24, 2008
Thank You! I REALLY needed this solution!
0
...
written by nfr, May 28, 2008
Your solution saved me a lot of work - thanks
0
...
written by Kate Fisher, June 05, 2008
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
0
...
written by Guido, June 21, 2008
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
0
...
written by Angelia, June 27, 2008
Hello, I don't find this option in Joomla 1.5.2. How do I create dynamic title in newest Joomla ?
62
...
written by Cory, June 27, 2008
Hi Angelina... you will want to read this article:

http://www.howtojoomla.net/content/view/86/1/
0
...
written by Shweta, July 15, 2008
Thanks for the info.
0
...
written by Mia, September 16, 2008
hi cory i have the same issue as Scott (February 16, 2007 post).
i did everything you told him (back end administrator) - but it's not working. i do not know any php but added the code on index.php. where does this code go?:
thnx
62
...
written by Cory, September 16, 2008
Hi Mia,

The index.php file I referred to in this article is the one in your template folder. For example, if your template is named "mytemplate", the index.php file would be found at yoursite.com/templates/mytemplate/index.php.

If you are using Joomla! 1.5, you will want to look at this article: http://www.howtojoomla.net/content/view/86/1/.

Thanks for visiting.

Cory
0
...
written by Nilay, September 18, 2008
Cory, how do I use $myPageTitle as arrived in your code as actual browser page title, as in between and ? If I uncheck dynamic page titles, the sitename appears as title on all pages. What I want is the dynamic title without the sitename, like I want the title of this page to be "How to Display Your Page Title in Your Template" and not "Cory Webb's How to Joomla! - How to Display Your Page Title in Your Template" Pls do reply, as I couldn't find (or understood) it in includes/frontend.php file.
62
...
written by Cory, September 18, 2008
If you are using Joomla! 1.5, you will want to look at this article: http://www.howtojoomla.net/content/view/86/1/.
0
...
written by Erik, September 30, 2008
Worked great, thanks!
0
...
written by wow gold, October 11, 2008
If you are using Joomla! 1.5, you will want to look at this article:
0
...
written by tour travel, January 13, 2009
This nice but how i get it for joolma 1.5
62
...
written by Cory Webb, January 13, 2009
Hi tour... try this article...

http://www.howtojoomla.net/content/view/86/1/
0
...
written by nikesh, January 14, 2009
Cory Webb

thanx

Write comment

security code
Write the displayed characters


busy

Join the HowToJoomla Community!

  • Create a profile
  • Connect with other members
    of the community
  • All for FREE

Sign up now!

 

Already a member? Login here.

HowToJoomla is proudly hosted by Eleven2

 
 
 

Search the Joomla! Community for Resources

 
 

Latest Community Activity

Yesterday
Cory and depika are now friends 12:47 PM
2 days ago
shashidhar replied in a discussion Need help 07:11 AM
shashidhar joined a group HowToJoomla! 07:10 AM
5 days ago
Shine Rankin replied in a discussion Need help 05:00 AM
Shine Rankin joined a group HowToJoomla! 04:59 AM
3 weeks ago
michael cuanico added Feeds application Jun 14
John joined a group HowToJoomla! Jun 10
 

Welcome to Our Newest Community Members

wil san
Ty Moyer
namal
krishan
shashidhar
leland
Jack Barakitis
Jim Livingstone
Robert Teufel
Amirul Azizi
James Hafner
Mahmood Anwar
 
 

Latest Comments

How to embed a Googl

I have embedded many times google maps to joomla, ...

Beginning Joomla! We

Thank you for given this great post.... ...

How to embed a YouTu

Hello, I have installed this plug in its working g ...

Blank Joomla! 1.5 Te

can rebrand to control panel logo? ...

How to Move Your Joo

Thank you for this. It worked like a dream for me ...