How to Display Joomla Page Titles in Your Template

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.

How to Display Joomla Page Titles in Your Template

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:

 

$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.

 

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.