Generate meta page title exactly as on h1 tag

Viewed 760

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?

4 Answers

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 :)

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.

Related