Score:-2

Set the canonical link rel via theme's template.php for a specific views page (not a node)

US flag

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');
}
berliner avatar
bd flag
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.
om62 avatar
md
I'm sorry. I'm not a developer but a website owner attempting on my own
Score:0
de flag

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');
}
om62 avatar
md
That did not work. The website white-screened and the editor says there is a syntax error on this line : )), 'link_rel_canonical');
om62 avatar
md
Thank you for your assistance, Jaypan. It worked by after I changed ```)), 'link_rel_canonical');``` to ``` ), 'link_rel_canonical');```
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.