3

I have an existing module, which I display from an article by using {loadposition custom_position}. The article is linked to a menu item. I want to be able to set a customized page title from this module, overwriting the page title set by the menu item.

I tried using

$document = JFactory::getDocument();
$document->setTitle('Set your title here');

But it does not set the page title.

Is it possible to set the page title from within a module, or can it only be done within a component.

Czeslaw
  • 116
  • 1
  • 7
  • 1
    What routing are you doing around your link in the module? You could probably use a custom router and set he options. – Elin Jul 16 '15 at 13:30
  • The question at http://stackoverflow.com/q/24865448/1983389 is similar and has an alternative answer using JavaScript. – Neil Robertson May 02 '16 at 05:38

2 Answers2

3

The code you wrote is correct, but maybe other modules / plugins are changing the title after you change it.

You may want to echo the title immediately after you set it, e.g.

$document = JFactory::getDocument();
$document->setTitle('Set your title here');
echo "<h1>" . JFactory::getDocument()->getTitle() . "</h1>";

If that is correct, you need to look for other modules / plugins that change it later.

Francesco Abeni
  • 4,190
  • 1
  • 19
  • 30
  • Thanks Francesco. You are right. The title returned by the echo statement is what I set. Therefore it must be overwritten later, probably by com_content, which is what I am using to display the article. I'll still need to figure out whether there is a way to stop com_content from overwriting the title. – Czeslaw Jul 16 '15 at 17:02
  • I fear you cannot. One way to do it is to write a plugin for that and make it executes at the right time, but that requires a bit more work. – Francesco Abeni Jul 16 '15 at 18:47
0
  1. You can create a "Hide Menu";
  2. Create the page link in this Menu;
  3. Change the page title in Menu item without any code line...
Max
  • 526
  • 6
  • 12
  • Thanks Max. This will set a static page title, but I am looking to dynamically set the page title from the module. – Czeslaw Jul 23 '15 at 19:49