You are here:  » Category meta title and description


Category meta title and description

Submitted by IG on Thu, 2018-11-29 07:46 in

Hi David,

I am currently tackling category meta titles and descriptions. Unfortunately, the German language makes it impossible to use one of the easier solutions I found in the forum.

I am using Category Hierarchy Mapping and it would be very beneficial, if I could add some extra fields there to define a separate category meta title and description for each category.

I believe the thread that I found here https://www.pricetapestry.com/node/5069 can be adapted for my needs. Would you be so kind and guide me?

Kind regards,
IG

Submitted by support on Thu, 2018-11-29 08:37

Hello IG,

Sure, you can do exactly the same as node 5069 for Category Hierarchy Mapping. First run the following dbmod.php script (from top level of Price Tapestry installation) to add new `title` and `meta_description` fields to the table;

<?php
  
require("includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."categories_hierarchy`
            ADD `title` VARCHAR(255) NOT NULL,
            ADD `meta_description` TEXT NOT NULL"
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

Then edit admin/categories_hierarchy_configure.php and look for the following code at line 68:

    $sql = "UPDATE `".$config_databaseTablePrefix."categories_hierarchy` SET alternates = '".database_safe($alternates)."' WHERE id='".database_safe($id)."'";

...and REPLACE with:

    $sql = "UPDATE `".$config_databaseTablePrefix."categories_hierarchy` SET alternates = '".database_safe($alternates)."',title='".database_safe($_POST["title"])."',meta_description='".database_safe($_POST["meta_description"])."' WHERE id='".database_safe($id)."'";

And then the following code at line 91:

  widget_formButtons(array("Save"=>TRUE),"categories_hierarchy.php");

...and REPLACE with:

  widget_textBox("Title","title",FALSE,$category["title"]);
  widget_textArea("Meta Description","meta_description",FALSE,$category["meta_description"],100,12);
  widget_formButtons(array("Save"=>TRUE),"categories_hierarchy.php");

And finally edit search.php and look for the following code at line 520:

    $header["title"] = $q;

...and REPLACE with:

    $header["title"] = $q;
    if ($parts[0]=="category")
    {
      $sql = "SELECT title,meta_description FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id='".database_safe($nodeInfo["id"])."'";
      if (database_querySelect($sql,$rowsch))
      {
        if ($rowsch[0]["title"])
        {
          $header["title"] = $rowsch[0]["title"];
        }
        if ($rowsch[0]["meta_description"])
        {
          $header["meta"]["description"] = $rowsch[0]["meta_description"];
        }
      }
    }

Cheers,
David.
--
PriceTapestry.com

Submitted by IG on Thu, 2018-11-29 20:57

Hi David,

It works perfectly and I got hungry to do more of the same myself :-)

I believe that I managed to successfully add the two fields also to Brand Mapping. However, I struggle with the last bit of code. Here's what I came up with:

Option 1:

    $header["title"] = $q;
    if ($parts[0]=="category")
    {
      $sql = "SELECT title,meta_description FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id='".database_safe($nodeInfo["id"])."'";
      if (database_querySelect($sql,$rowsch))
      {
        if ($rowsch[0]["title"])
        {
          $header["title"] = $rowsch[0]["title"];
        }
        if ($rowsch[0]["meta_description"])
        {
          $header["meta"]["description"] = $rowsch[0]["meta_description"];
        }
      }
    }
    if (($parts[0]=="brand") || ($parts[0]=="merchant"))
    {
      $sql = "SELECT title,meta_description FROM `".$config_databaseTablePrefix."brands` WHERE name='".database_safe($parts[1])."'";
      if (database_querySelect($sql,$rowsch))
      {
        if ($rowsch[0]["title"])
        {
          $header["title"] = $rowsch[0]["title"];
        }
        if ($rowsch[0]["meta_description"])
        {
          $header["meta"]["description"] = $rowsch[0]["meta_description"];
        }
      }
    }

Options 2:

    if (isset($parts[1]))
    {
      switch($parts[0])
      {
        case "category":
              $sql = "SELECT title,meta_description FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id='".database_safe($nodeInfo["id"])."'";
      if (database_querySelect($sql,$rowsch))
      {
        if ($rowsch[0]["title"])
        {
          $header["title"] = $rowsch[0]["title"];
        }
        if ($rowsch[0]["meta_description"])
        {
          $header["meta"]["description"] = $rowsch[0]["meta_description"];
        }
      }
          break;
        case "brand":
        case "merchant":
              $sql = "SELECT title,meta_description FROM `".$config_databaseTablePrefix."brands` WHERE name='".database_safe($parts[1])."'";
      if (database_querySelect($sql,$rowsch))
      {
        if ($rowsch[0]["title"])
        {
          $header["title"] = $rowsch[0]["title"];
        }
        if ($rowsch[0]["meta_description"])
        {
          $header["meta"]["description"] = $rowsch[0]["meta_description"];
        }
      }
          break;
      }
    }
    else
    {
      $header["title"] = $q;
    }

Both options work for categories and for brands, but not for the merchants. As you might remember, I am using Brand Mapping also for merchant descriptions and I would like to do the same for these two additional fields.

Cheers,
IG

Submitted by support on Fri, 2018-11-30 08:56

Hi,

Your code looks perfect, I couldn't see anything obviously wrong so I tried it quickly without any changes on my test server and was able to set the title and meta_description for a merchant from Brand Mapping.

Does the merchant name contain any accented characters, I know you're working on a German site so I'm wondering if it's a character set / collation issue?

Cheers,
David.
--
PriceTapestry.com

Submitted by IG on Fri, 2018-11-30 09:44

Hi David,

I managed to make it work. I made a tiny mistake in the code before and the fields where not saved.

Now that I am getting good at this (probably I should say better), I was wondering if the same can be done for the category, brand and page title?

Cheers, Ivo

Submitted by support on Fri, 2018-11-30 10:49

Hi Ivo,

The above covers custom title and meta description for category, brand and merchant pages - what other page types did you want to apply the same principle to?

Cheers,
David.
--
PriceTapestry.com

Submitted by IG on Fri, 2018-11-30 11:48

I would like to have a custom h1 title for category, brand and merchant pages that I can define as well in category hierarchy and brand mapping.

Submitted by support on Fri, 2018-11-30 12:26

Hello Ivo,

It would be no problem to add a heading value to use; first the dbmod.php

<?php
  
require("includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."categories_hierarchy`
            ADD `heading` VARCHAR(255) NOT NULL"
;
  
database_queryModify($sql,$result);
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."brands`
            ADD `heading` VARCHAR(255) NOT NULL"
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

Then if you;

i) extend the modifications to...
admin/categories_hierarchy_configure.php and html/brands_configure.php
...to add the heading field to the UPDATE query and form

ii) include the heading field in the SELECT query used in search.php, for example:

  $sql = "SELECT title,meta_description,heading FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id='".database_safe($nodeInfo["id"])."'";

(let me know if you're not sure about any of the changes of course)

You can now display the heading at the top of html/searchresults.php by checking if $rowsch is set, $rowsch["heading"] is not empty, and if you would like to avoid duplicate h1 tags across the pages, only for page 1 using for example:

<?php
  if (isset($rowsch) && ($rowsch[0]["heading"]) && ($page==1))
  {
    print "<h1>".$rowsch[0]["heading"]."</h1>";
  }
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by IG on Fri, 2018-11-30 14:16

Hi David,

All done. Thank you so much for your support.

Is it possible to add a page number a the end of the heading as it is the case in the title?

Cheers, IG

Submitted by support on Fri, 2018-11-30 14:38

Hello Ivo,

Sure - based on the html/searchresults.php modification above, to remove the page==1 condition and then to include the page for pages > 1 in the output have a go with something like;

<?php
  
if (isset($rowsch) && ($rowsch[0]["heading"]))
  {
    print 
"<h1>".$rowsch[0]["heading"].($page>1?" | Page ".$page:"")."</h1>";
  }
?>

This construct:

($page>1?" | Page ".$page:"")

...is a "ternary" (search for that term at php.net/manual/en/language.operators.comparison.php for more info) and is essentially a shorthand form of if/then/else which is the most elegant way to include conditional text within a string, ideal for this kind of scenario...

Cheers,
David.
--
PriceTapestry.com