4

I need to insert hreflang tags in the head tags, but I only need these on the homepage. At the moment I'm inserting the hreflang tags into the head via the html.tpl.php template, but this puts the code on all pages in the site. How can I edit the head for just one page? I'm using Drupal 7.

1
  • html--front.tpl.php
    – GwenM
    Commented Jun 11, 2015 at 16:05

4 Answers 4

3

You can use:

<?php if ($is_front): ?>
 <!-- code here-->
<?php endif; ?>

In your html.tpl.php and it'll only affect the homepage.

1
2

You can set the condition for front page in html.tpl.php, so set your required flag. Use the condition like:

<?php
  if (drupal_is_front_page()){
    // print the desired code
  }
?>
1

I suggest you three solution.

  1. Duplicate your html.tpl.php and rename one of them to html--front.tpl.php, apply your change( hreflang tags ) then clear the cache.

  2. another solution is with simple condition apply what you want in html.tpl.php, I mean

    put your markups here

But $is_front not exists in html.tpl.php varibles and by default available in page templates,you can add it manually by using template_preprocess_html

function hook_preprocess_html(&$vars) {
  $vars['is_front'] = drupal_is_front_page();
}
  1. use PHP function in your html.tpl.php

( although if you use PHP function in theme, you should forget using PHP in template in Drupal 8 )

<?php if (drupal_is_front_page()): ?>
 put your markups here
<?php endif; ?>
0

use html_head_alter in template.php and add conditions

 function your theme_html_head_alter(&$head_elements) {

$remove = array(
        'apple-touch-icon57',
        'apple-touch-icon72',
        'apple-touch-icon114'
);

foreach ($remove as $key) {
    if (isset($head_elements[$key])) {
        unset($head_elements[$key]);
    }
}   

//add
$appleIcon57px = array('#tag' => 'link', '#type' => 'html_tag', '#attributes' => array('rel' => 'apple-touch-icon', 'href' => '/misc/AMU-NUMERIQUE-ICONE-57.png', 'type' => 'image/png', 'media' => 'screen and (resolution: 163dpi)'),);
$appleIcon72px = array('#tag' => 'link','#type' => 'html_tag', '#attributes' => array('rel' => 'apple-touch-icon', 'href' => '/misc/AMU-NUMERIQUE-ICONE-72.png', 'type' => 'image/png', 'media' => 'screen and (resolution: 132dpi)'),);
$appleIcon114px = array('#tag' => 'link','#type' => 'html_tag', '#attributes' => array('rel' => 'apple-touch-icon', 'href' => '/misc/AMU-NUMERIQUE-ICONE-114.png', 'type' => 'image/png', 'media' => 'screen and (resolution: 326dpi)'),);


$head_elements['apple-touch-icon57']=$appleIcon57px;
$head_elements['apple-touch-icon72']=$appleIcon72px;
$head_elements['apple-touch-icon114']=$appleIcon114px;
}

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.