我是drupal 8的新手,想在我的drupal 8网站上添加meta标签.
我这样做了:
在我的THEME.theme文件中添加了以下代码
/**
* Implements hook_page_attachments_alter().
*
* Include meta tags and fonts using attachment method.
*/
function bartik_page_attachments_alter(array &$page) {
// echo "<pre>"; echo "BEFORE"; print_r($page);
// Include fonts.
$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);
// Set up meta tags.
// Modern IE & chrome-frame rendering engine tag.
$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'];
//echo "<pre>"; echo "AFTER"; print_r($page);
}
Run Code Online (Sandbox Code Playgroud)
此代码只附加登录页面的元标记,即在我的情况下是:
mysite.com/user/login
Run Code Online (Sandbox Code Playgroud)
我在哪里做错了?我应该怎么做才能将它附加到整个网站
小智 9
我在头部添加元标记的做法是:
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'];
};
Run Code Online (Sandbox Code Playgroud)
它导致了
<meta http-equiv="x-ua-compatible" content="ie=edge">
Run Code Online (Sandbox Code Playgroud)
也许不是最好的解决方案,但它的工作:)
祝好运
以下代码适用于使用基本页面内容类型中的自定义字段覆盖元标题和说明:
/**
* 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'];
}
}
}
Run Code Online (Sandbox Code Playgroud)
快乐的编码!
归档时间: |
|
查看次数: |
10706 次 |
最近记录: |