How-To Categories
Recent How-To's
News Feed E-mail
Login
| How to Display Your Page Title in Your Template |
|
|
|
| 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 TitleYou 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 DynamicJoomla! 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 TitleAt 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 TemplateThe 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.
Set as favorite
Bookmark
Email This
Hits: 20401 Trackback(0)
Comments (72)
![]()
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
Cory: ... http://www.howtojoomla.net
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!
![]() 3 report abuse
vote down
vote up
December 03, 2006
Votes: +0
Jeroen: ... http://www.fluoline.net
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
Joomlaz: ... http://www.zechmann.de
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
Miftahx: ... http://vespa-indonesia.com
Thanks for the info Cory, just apply it at Vespa Community Website.
![]() 7 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
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
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
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
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 ![]() 17 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
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
chris_: ... http://netstuff.ro
thanks a lot for sharing...
saved me a lot of trouble ![]() 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
Fireflight: ... http://www.darlinghomeforkids.ca/
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
vitovito: ... http://www.vittorioursini.eu
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
vitovito: ... http://www.vittorioursini.eu
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
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.
![]() 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
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 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
Hema: ... http://www.ieg.gov.in
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
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
Zaur: ... http://www.pdesigner.net
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
paul: ... http://www.wms.to
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
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
Nate: ... http://christianfreedom.org
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!
![]() 52 report abuse
vote down
vote up
December 14, 2007
Votes: +0
Layne: ... http://www.ccipipeline.com
Do you think you could write the code to display the alias?
53 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
Layne: ... http://www.ccipipeline.com
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
Layne: ... http://www.ccipipeline.com
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
![]() 65 report abuse
vote down
vote up
May 09, 2008
Votes: +0
shawn: ... http://www,jvblogger.com
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
Kate Fisher: ... http://newsite.wildwoodtrust.org
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
Angelia: ... http://www.anti-aging-secrets.net
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
Shweta: ... http://www.shweta.lotlikar.in
Thanks for the info.
72 report abuse
vote down
vote up
July 15, 2008
Votes: +0 Write comment
|
| < Prev | Next > |
|---|















