Actions

Difference between revisions of "Customising the dynamic page title"

From Joomla! Documentation

(Updated with more info.)
m (Fixed mistake, hopefully)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This will allow you to change the global page title across the entire site. You may customize it to your own liking within your own templates files.
+
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 <strong>template</strong> index.php, although most people tend to insert it near the top or within the <nowiki><head></nowiki> element.
  
The code needs to go just after the opening <nowiki><head></nowiki> element and before <nowiki><jdoc:include type="head" /></nowiki> element.
+
To get the current page title:
 +
<source lang="php">
 +
<?php
 +
$title = $this->getTitle();
 +
?>
 +
</source>
  
 +
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:
 +
<source lang="php">
 +
<?php
 +
$app = JFactory::getApplication();
 +
$this->setTitle( $title . ' - ' . $app->getCfg( 'sitename' ) );
 +
?>
 +
</source>
  
Document title: <strong><nowiki>$document->title</nowiki></strong>
+
Combining these two pieces of code together, you can do this:
 
+
Sitename: <strong><nowiki>$mainframe->getCfg('sitename')</nowiki></strong>
+
 
+
 
+
How it would look(currently set to "Document title - Sitename"):
+
 
<source lang="php">
 
<source lang="php">
<?php
+
<?php
$document =& JFactory::getDocument();
+
$app = JFactory::getApplication();
$document->setTitle($document->title . " - " . $mainframe->getCfg('sitename'));
+
$this->setTitle( $this->getTitle() . ' - ' . $app->getCfg( 'sitename' ) );
?>
+
?>
 
</source>
 
</source>
  
 
+
Here's a more complete example of what a template would look like with this code embedded in it:
Example (template coding may vary):
+
 
<source lang="php">
 
<source lang="php">
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<!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">
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
 
<head>
<?php
+
<?php
$document =& JFactory::getDocument();
+
$app = JFactory::getApplication();
$document->setTitle($document->title . " - " . $mainframe->getCfg('sitename'));
+
$this->setTitle( $this->getTitle() . ' - ' . $app->getCfg( 'sitename' ) );
?>
+
?>
 
<jdoc:include type="head" />
 
<jdoc:include type="head" />
 +
 
.... rest of template file.
 
.... rest of template file.
</source>
+
 
[[Category:Tips and tricks]]
+
</source><noinclude>[[Category:Tips and tricks]][[Category:Tips and tricks 1.5]][[Category:Tutorials]][[Category:Template Development]]</noinclude>
[[Category:Tips and tricks 1.5]]
+
[[Category:Tutorials]][[Category:Component Development]]
+

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.