【问题标题】:How add something in "<head>" with Drupal 7.x?如何使用 Drupal 7.x 在“<head>”中添加内容?
【发布时间】:2016-02-26 15:00:34
【问题描述】:

我有很大的疑问...我添加内容的方式是否正确(例如发布者 google plus 的链接,或元标记或外部资源 css):

function mysubtheme_page_alter($page) {

$viewport = array(
    '#type' => 'html_tag',
     '#tag' => 'meta',
     '#attributes' => array(
         'name' =>  'viewport',
         'content' =>  'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no')
 );
 drupal_add_html_head($viewport, 'viewport');


 $googleplus = array(
     '#type' => 'html_tag',
     '#tag' => 'link',
     '#attributes' => array(
        'href' =>  'https://plus.google.com/+google-plus',
        'rel' =>  'publisher')
 );
 drupal_add_html_head($googleplus, 'googleplus');


 $pinterest = array(
     '#type' => 'html_tag',
     '#tag' => 'meta',
     '#attributes' => array(
        'name' =>  'p:domain_verify',
        'content' =>  '7680eb52326ae9ee9e415d0ad')
 );
 drupal_add_html_head($pinterest, 'pinterest'); 


 $fontawesome = array(
     '#type' => 'html_tag',
     '#tag' => 'link',
     '#attributes' => array(
        'href' =>  '/sites/font-awesome.min.css',
        'rel' =>  'stylesheet')
 );
 drupal_add_html_head($fontawesome, 'fontawesome');
}

我希望你能帮助我:) 对不起我的英语

edit 我问这个也是因为这些是我最新的更改,现在我注意到如果我登录并使用Firefox 查看我的网站,则没有 CSS!荒诞!

现在我有 3 种方法:

  1. 不正确如何在head 中添加内容
  2. 发行 Firefox (44.0.2)
  3. 问题模块 ADVANCED CSS/JS AGGREGATION

EDIT 2.0 这是 firefox 的问题(我重置它并解决了问题)......但是我想知道这种添加 head 的方式是否正确;)

【问题讨论】:

    标签: php drupal drupal-7 head


    【解决方案1】:

    drupal_add_html_head 将输出添加到 HTML 页面的 HEAD 标记。只要不发送标头,就可以调用此函数。

    更多信息https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_html_head/7

    【讨论】:

    • 但是我的代码是对的,drupal_add_html_head 在函数 mysubtheme_page_alter($page) 中?
    • 只要不发送header就可以调用这个函数。尝试将您的代码移动到您的自定义模块中(可能是 hook_init )。感谢您的反馈
    【解决方案2】:

    我发现的最简单的方法是在我的 template.php 中删除以下内容

        function THEMENAME_preprocess_html(&$variables) {
          if(drupal_is_front_page()) {
            $meta_description = array(
                    '#type' => 'html_tag',
                    '#tag' => 'meta',
                    '#attributes' => array(
                        'name' => 'description',
                        'content' =>  'blah blah blah'
                    )
            );
            $meta_keywords = array(
                    '#type' => 'html_tag',
                    '#tag' => 'meta',
                    '#attributes' => array(
                        'name' => 'keywords',
                        'content' =>  'some, keywords'
                    )
            );
            drupal_add_html_head( $meta_description, 'meta_description' );
            //drupal_add_html_head( $meta_keywords, 'meta_keywords' );
    
            drupal_add_html_head_link(array(
              'rel' => 'publisher',
              'href' => 'https://plus.google.com/xxxxxxxxx',
            ));
          }
        }
    

    Publisher 只需要在首页因此 drupal_is_front_page

    【讨论】:

    • 如果也添加到网站的其他页面?有问题吗?
    猜你喜欢
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2010-12-26
    • 1970-01-01
    • 2013-10-13
    相关资源
    最近更新 更多