Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate meta page title exactly as on h1 tag

Tags:

html

php

joomla

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?

like image 594
Ruchika Avatar asked Jul 29 '17 05:07

Ruchika


People also ask

Can meta title be the same as H1?

Every article gets two titles—the meta title and the on-page title (H1). These two titles can be exactly the same.

Should page title be same as H1?

To avoid any conflicts and be consistent with best SEO practices, make sure that your page title and H1 tags have the same value. It's not the end of the world if you have multiple H1 tags on a page but for maximum SEO optimization, it's better to have only one.

Is SEO title same as H1?

Purpose of the post title and the SEO title It is important to realize that your SEO title doesn't have the same purpose as the title of your post or page (your H1 heading). Your post title is meant for people that are already on your site. It's telling them what your post or page is about.

Is a H1 tag a meta tag?

A meta title tag is an HTML element used to specify the title of a webpage. It's written as <title> under the <head> section of your site's code. People often confuse these with H1 tags because both the title and H1 tag describe what a page is about. The main difference is where they appear.


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

like image 92
pycvalade Avatar answered Sep 29 '22 16:09

pycvalade