-2

I do not want to use the D7 Metatag or the MetaTag Quick modules. How can I specify a single canonical link rel for a single URL which is not a node (a view page) via the theme's template.php?

I've tried this but it has a syntax error:

if (isset($_GET['q']) && preg_match("/^products\category/", $_GET['q']) {
  drupal_add_html_head(array(
    "#tag" => "link",
    "#attributes" => array(
      "rel" => "canonical",
      "href" => "https://domain.com/products/category",
    ),
  ), 'link_rel_canonical');
}
2
  • 1
    If it's a syntax error, then this question is not specific to Drupal and doesn't belong on this site. Also, you should be able to see what syntax error this is and fix it using common tools, like error reporting in the page or server logs. Jaypan already pointed out the missing closing bracket for the if condition.
    – berliner
    Mar 23, 2023 at 17:10
  • I'm sorry. I'm not a developer but a website owner attempting on my own
    – om62
    Mar 23, 2023 at 17:22

1 Answer 1

0

As you said, you have a syntax error. You should be able to use this:

if (preg_match("/^products\/category/", $_GET['q'])) {
  drupal_add_html_head(array(
    "#tag" => "link",
    "#attributes" => array(
      "rel" => "canonical",
      "href" => "https://domain.com/products/category",
    ),
  )), 'link_rel_canonical');
}
2
  • That did not work. The website white-screened and the editor says there is a syntax error on this line : )), 'link_rel_canonical');
    – om62
    Mar 23, 2023 at 17:17
  • Thank you for your assistance, Jaypan. It worked by after I changed )), 'link_rel_canonical'); to ``` ), 'link_rel_canonical');```
    – om62
    Mar 23, 2023 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.