16

Site based on Joomla. I have many pages where h1 header is mentioned as product detail and displayed based on product details through PHP. There are 2 files: default.php and view.html.php.

default.php :

<h1>Used <?php echo $this->CatName; ?> <?php echo $this->prodDet->prod_name;?> Toy for Sale </h1>

This correctly display the h1 tag. I want to generate meta title of the page and use this h1 output as generated in view.html.php. This line defines the title of the page :

$this->document->setTitle($title);

And this line defines header h1 :

"{$this->item->heading}";

Complete code :

protected function _prepareDocument()
{
  $app = JFactory::getApplication();
  $menus = $app->getMenu();
  $title = null;

  // Because the application sets a default page title,
  // We need to get it from the menu item itself
  $menu = $menus->getActive();

  if ($menu)
  {
    $this->params->def('page_heading', $this->params->get('page_title', $menu->title));
  }
  else
  {
    $this->params->def('page_heading', JText::_('COM_USEDCAR_DEFAULT_PAGE_TITLE'));
  }

  $title = $this->params->get('page_title', '');

  if (empty($title))
  {
    $title = $app->get('sitename');
  }
  elseif ($app->get('sitename_pagetitles', 0) == 1)
  {
    $title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
  }
    elseif ($app->get('sitename_pagetitles', 0) == 2)
  {
    $title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
  }

  $title = "{$this->item->heading}";
  $this->document->setTitle($title);

  if ($this->params->get('menu-meta_description'))
  {
    $this->document->setDescription($this->params->get('menu-meta_description'));
  }

  if ($this->params->get('menu-meta_keywords'))
  {
    $this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
  }

  if ($this->params->get('robots'))
  {
     $this->document->setMetadata('robots', $this->params->get('robots'));
  }
}

Output in title tag is heading. How to put this h1 tag output instead of $title?

user4157124
  • 2,809
  • 13
  • 27
  • 42
Ruchika
  • 503
  • 1
  • 8
  • 26
  • 2
    Just above the line: `$this->document->setTitle($title);` you can redefine `$title` to be anything you want. Just make sure you don't break the general logic of that function. – Difster Jul 29 '17 at 05:12
  • @ Difster - see updated, the correct title tag is not displayed, any help – Ruchika Jul 29 '17 at 06:27
  • Is this joomla? If yes, you can set page title in the menu options – Alaksandar Jesus Gene Jul 29 '17 at 06:29
  • Yes this is joomla, but its a custom component and there is no as such page title from menu as there are 100s of product pages inside this menu and need page title of these pages as similar to h1 tag as created on the page. – Ruchika Jul 29 '17 at 06:36
  • can any one help pls – Ruchika Jul 31 '17 at 09:43
  • can you use java script in the page ... hackish, but trivial – ArtisticPhoenix Aug 05 '17 at 06:16
  • Did you create that line `$title = "{$this->item->heading}";` by yourself? - It looks as if a plugin should be used to display the H1 header as Meta page title. Maybe the associated plugin is disabled. Or you can write a plugin in Joomla to generate this functionality. – Tom Kuschel Aug 06 '17 at 06:26
  • Yes - was trying by writing $title = "{$this->item->heading}"; as picked through google but its not working – Ruchika Aug 06 '17 at 09:12
  • @Ruchika : you should set `$title = 'Used ' . $this->CatName . ' ' . $this->prodDet->prod_name . ' Toy for Sale ';` – Irfan Aug 07 '17 at 08:39

4 Answers4

1

Here's what the title portion of your code does:

// getting title from params
  $title = $this->params->get('page_title', '');

// trying to get it right
  if (empty($title))
  {
    $title = $app->get('sitename');
  }
  elseif ($app->get('sitename_pagetitles', 0) == 1)
  {
    $title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
  }
    elseif ($app->get('sitename_pagetitles', 0) == 2)
  {
    $title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
  }

// overwrite everything above with some value, making above code useless
  $title = "{$this->item->heading}";
  $this->document->setTitle($title);

I might be wrong but if I recall correctly, if a value doesn't exist it will return the variable name when cast into a string. Here "heading" might be empty.

You might want to change your code to something like this:

[...]

if(!title){
  if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading){
    $title = $this->item->heading;
  } else {
    $title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
  }
}
$this->document->setTitle($title);

You might as well like to save the title to session and reuse it everywhere:

[...]
$this->document->setTitle($title);

// save title to session
$_SESSION['page_title'] = $title;

and update the previous loop:

// getting title from params
  $title = (isset($_SESSION['page_title']) && $_SESSION['page_title'])? $_SESSION['page_title'] : $this->params->get('page_title', '');

if (empty($title)){
[...]

Full code would be something like that:

[...]
session_id() || session_start();

$title = (isset($_SESSION['page_title']) && $_SESSION['page_title'])? $_SESSION['page_title'] : $this->params->get('page_title', '');

if(!title){
  if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading){
    $title = $this->item->heading;
  } else {
    $title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
  }
}

if (empty($title))
{
  $title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
  $title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
  elseif ($app->get('sitename_pagetitles', 0) == 2)
{
  $title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}

$_SESSION['page_title'] = $title;
$this->document->setTitle($title);
[...]

You might as well just ditch everything and go like that if you'd like:

[...]
$title = $this->params->get('page_title', '');

if(!title){
  if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading) {
    $title = $this->item->heading;
  } elseif(
        property_exists($this, 'CatName') && 
        property_exists($this, 'prodDet') && 
        property_exists($$this->prodDet, 'prod_name') && 
        $this->CatName && 
        $this->prodDet->prod_name
      ){
    $title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
  } else {
    $title = $app->get('sitename');
  }
}

$this->document->setTitle($title);
[...]

Code is untested but it should put you on the right track :)

pycvalade
  • 181
  • 9
0

Why don't you just send your h1 content to your php-document as GET parameter and then just output the it using echo inside the title tag? Unless you avoid dinamic echoing, this could be a fine solution for outputting text as title.

0

I would abstract away the logic of constructing the title/header to some function and then use this function to construct the title in both places.

function constructTitle($catName, $prodName) {
    return "Used {$catName} {$prodName} Toy for Sale";
}

...

[in default.php]
<h1><?php echo constructTitle($this->CatName, $this->prodDet->prod_name); ?></h1>

[in view.html.php]
$this->document->setTitle(constructTitle(..., ...));

This allows you to have a single point to format your title while using it in several places.

The function needs to, obviously, be place in such position so that it can be accessed in both places and you need to have some way to get category name and product name in view.html.php. Im not familiar enough with joomla to know these things.

Edit: To clarify, there is no real way to "extract" the title from the default.php as it is dynamic. You would need to process the php file then maybe you could do some regex magic, but this is in no way the proper solution to the problem.

Artog
  • 1,132
  • 1
  • 13
  • 25
0

you can just send your h1 content to your php-document as GET parameter and then output the it using echo in the title tag? Unless you avoid dynamic echoing,it would work.

Akanimo
  • 39
  • 5