How to Display Your Page Title in Your Template
Written by Cory |
October 04, 2006
|
(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)
written by jfh, October 12, 2006
written by Cory, October 12, 2006
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.
written by Jeroen, December 18, 2006
written by Joomlaz, February 10, 2007
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
written by Scott, February 16, 2007
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?
written by Av, February 21, 2007
written by Av, February 23, 2007
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
written by Av, February 23, 2007
Thanks again for the information.I guess i need to download that component and install in my site.....do i need to edit it??????
written by Amit D, March 19, 2007
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
written by Fireflight, March 27, 2007
written by Yunus, March 31, 2007
written by Fireflight, April 08, 2007
Any ideas?
written by Fireflight, April 09, 2007
written by vitovito, April 16, 2007
Thanks
written by vitovito, April 16, 2007
-
written by albert, April 25, 2007
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
written by Tempus Thales, May 31, 2007
written by jo, July 04, 2007
Reply me
written by Ernst, July 15, 2007
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
written by Ernst, July 15, 2007
Regards, ernst
written by Adhe, July 16, 2007
Thanks. I was looking for this
How about if I have that title in image, how can I put that image title?
Thanks before.
written by Hema, July 18, 2007
written by Nirav, July 19, 2007
written by Zaur, September 20, 2007
written by mossaab, September 28, 2007
written by thanks man !this tips help me more, October 11, 2007
written by Matt Hayashida, November 10, 2007
written by Matt Hayashida, November 10, 2007
written by free joomla template, November 24, 2007
written by Nate, December 03, 2007
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
written by Jennifer, December 14, 2007
written by Henry, December 19, 2007
written by Layne, December 19, 2007
written by kdobrev, February 21, 2008
written by Nikolai, March 08, 2008
written by Andy, March 17, 2008
written by gray, April 30, 2008
written by Jennifer, May 04, 2008
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!
written by Ihwan, May 10, 2008
written by Kate Fisher, June 05, 2008
Hopeful of another clever workaround
Kate
written by Guido, June 21, 2008
Maybe you can help me. That is the last problem on my page to go public.
Best Regards from New Zealand,
Guido
written by Angelia, June 27, 2008
written by Mia, September 16, 2008
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
written by Nilay, September 18, 2008
written by wow gold, October 11, 2008
Write comment
Join the HowToJoomla Community!
- Create a profile
- Connect with other members
of the community - All for FREE
Already a member? Login here.


