Difference between revisions of "Customising the dynamic page title"

From Joomla! Documentation

(New page: '''Customize the dynamic page title''' To customize the page title to add the site name followed by the document title, edit index.php in the template that you are currently using. For ex...)
 
m (Fixed mistake, hopefully)
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''Customize the dynamic page title'''
+
A common requirement is to customise the way in which the page title is displayed, For example, including the site name as part of the page title. The code on this page will allow you to change the global page title across the entire site. You may customise it to your own liking within your own template filesThe code can be placed anywhere in your <strong>template</strong> index.php, although most people tend to insert it near the top or within the <nowiki><head></nowiki> element.
To customize the page title to add the site name followed by the document title, edit index.php in the template that you are currently usingFor example, directly after the <head> tag add the following to show "Site Name - Page Name" as the browser's page title:
 
  
<?php
+
To get the current page title:
$document =& JFactory::getDocument();
+
<source lang="php">
$document->setTitle($mainframe->getCfg('sitename') . " - " . $document->title);
+
<?php
?>
+
$title = $this->getTitle();
 +
?>
 +
</source>
  
[[Category:Tips and tricks]]
+
Similarly, you use <tt>setTitle</tt> to set a new page title.  For example, to append the site name to the regular page title, you can use code like this:
[[Category:Tips and tricks 1.5]]
+
<source lang="php">
 +
<?php
 +
$app = JFactory::getApplication();
 +
$this->setTitle( $title . ' - ' . $app->getCfg( 'sitename' ) );
 +
?>
 +
</source>
 +
 
 +
Combining these two pieces of code together, you can do this:
 +
<source lang="php">
 +
<?php
 +
$app = JFactory::getApplication();
 +
$this->setTitle( $this->getTitle() . ' - ' . $app->getCfg( 'sitename' ) );
 +
?>
 +
</source>
 +
 
 +
Here's a more complete example of what a template would look like with this code embedded in it:
 +
<source lang="php">
 +
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 +
<html xmlns="http://www.w3.org/1999/xhtml">
 +
<head>
 +
<?php
 +
$app = JFactory::getApplication();
 +
$this->setTitle( $this->getTitle() . ' - ' . $app->getCfg( 'sitename' ) );
 +
?>
 +
<jdoc:include type="head" />
 +
 
 +
.... rest of template file.
 +
 
 +
</source><noinclude>[[Category:Tips and tricks]][[Category:Tips and tricks 1.5]][[Category:Tutorials]][[Category:Template Development]]</noinclude>

Latest revision as of 06:56, 20 May 2011

A common requirement is to customise the way in which the page title is displayed, For example, including the site name as part of the page title. The code on this page will allow you to change the global page title across the entire site. You may customise it to your own liking within your own template files. The code can be placed anywhere in your template index.php, although most people tend to insert it near the top or within the <head> element.

To get the current page title:

<?php
$title = $this->getTitle();
?>

Similarly, you use setTitle to set a new page title. For example, to append the site name to the regular page title, you can use code like this:

<?php
$app = JFactory::getApplication();
$this->setTitle( $title . ' - ' . $app->getCfg( 'sitename' ) );
?>

Combining these two pieces of code together, you can do this:

<?php
$app = JFactory::getApplication();
$this->setTitle( $this->getTitle() . ' - ' . $app->getCfg( 'sitename' ) );
?>

Here's a more complete example of what a template would look like with this code embedded in it:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
$app = JFactory::getApplication();
$this->setTitle( $this->getTitle() . ' - ' . $app->getCfg( 'sitename' ) );
?>
<jdoc:include type="head" />

.... rest of template file.