【问题标题】:Add meta tag to <head>将元标记添加到 <head>
【发布时间】:2016-03-10 10:25:01
【问题描述】:

我想向我的网站添加元标记。

我在我的 THEME.theme 文件中添加了这段代码并清除了缓存。

/**
 * Implements hook_page_attachments_alter().
 *
 * Include meta tags and fonts using attachment method.
 */
function bartik_page_attachments_alter(array &$page) {
  $font_attributes = array(
    'rel' => 'stylesheet',
    'type' => 'text/css',
    'href' => 'https://fonts.googleapis.com/css?family=Lato:400,100,100italic,300,300italic,400italic,700',
  );

  $page['#attached']['html_head_link'][] = array($font_attributes);

  $rendering_meta = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'SKYPE_TOOLBAR',
      'content' => 'SKYPE_TOOLBAR_PARSER_COMPATIBLE',
    ),
  );

  $page['#attached']['html_head'][] = [$rendering_meta, 'rendering_meta'];
}

此代码只是将元标记附加到登录页面。

如何将元标记添加到所有网站页面?

【问题讨论】:

    标签: drupal drupal-8


    【解决方案1】:

    我为头部添加元标记所做的是:

    function theme_preprocess_html(&$variables) {
    
      $xuacompatible = [
        '#tag' => 'meta',
        '#attributes' => [
          'http-equiv' => 'x-ua-compatible',
          'content' => 'ie=edge',
        ],
      ];
    
    
      $variables['page']['#attached']['html_head'][] = [$xuacompatible, 'x-ua-compatible'];
    }; 
    

    结果

    <meta http-equiv="x-ua-compatible" content="ie=edge">
    

    也许不是最好的解决方案,但它有效:)

    祝你好运

    【讨论】:

      【解决方案2】:

      以下代码对我有用,可以使用基本页面内容类型中的自定义字段覆盖元标题和描述:

      /**
       * Implements hook_preprocess_html() for html templates.
       */
      function theme_preprocess_html(&$variables)
      {
          // Add META tag title + description from back-office node basic page field
          $node = \Drupal::routeMatch()->getParameter('node');// Load the node entity from current route
          if ($node) {
              if ($node->getType() === 'page') {// Check if node type is basic page
                  $title = [
                      '#tag' => 'meta',
                      '#attributes' => [
                          'name' => 'title',
                          'content' => $node->get('field_custom_meta_title')->value,
                      ],
                  ];
                  $description = [
                      '#tag' => 'meta',
                      '#attributes' => [
                          'name' => 'description',
                          'content' => $node->get('field_custom_meta_description')->value,
                      ],
                  ];
                  $variables['page']['#attached']['html_head'][] = [$title, 'title'];
                  $variables['page']['#attached']['html_head'][] = [$description, 'description'];
              }
          }
      }
      

      编码愉快!

      【讨论】:

        【解决方案3】:

        也许试试这个模块https://www.drupal.org/project/metatag 还是创建预处理函数?

        【讨论】:

          【解决方案4】:

          我想对 Antoine 的回答做一个小的改动,因为标题元标记不会像他描述的那样起作用。需要更改数组以允许

          命名约定。这是我如何在自定义模块中工作的示例: <pre><code>function meta_manager_preprocess_html(&$variables) { $node = \Drupal::routeMatch()->getParameter('node'); if ($node) { // Make sure we are in a node $title = [ '#tag' => 'title', // Set title for element '#value' => 'Sample Title' // Set value for title ]; $description = [ '#tag' => 'meta', // Set meta for element '#attributes' => [ // Set attributes for meta 'name' => 'description', 'content' => 'Sample Description', ], ]; $variables['page']['#attached']['html_head'][] = [$title, 'title']; $variables['page']['#attached']['html_head'][] = [$description, 'description']; } } </code></pre> <p><strong>注意:</strong> 还请记住,默认情况下 html.html.twig 文件中有一个 </p> <title> 标记,因此如果您这样做,那么您可能需要将其注释掉或删除它首先。 <p>我也在开发这个模块,以在每个页面的单个节点上实现自定义元标记信息,以允许更多自定义,但这适用于通用应用程序。或者,您可以使用 Antoine 使用的示例,他将自定义字段添加到他的内容类型中。</p>

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-01-12
            • 1970-01-01
            • 2016-04-13
            • 1970-01-01
            • 1970-01-01
            • 2011-08-16
            • 1970-01-01
            相关资源
            最近更新 更多