gpt4 book ai didi

drupal - 如何将当前节点的分类法的主体类添加到 Drupal 7 主题

转载 作者:行者123 更新时间:2023-12-04 06:37:55 24 4
gpt4 key购买 nike

有谁知道如何或可以指导我如何为当前节点的分类术语添加 body css 类的正确方向?即 <body class="term-dogs">其中“dogs”是分类术语名称。它也可以只是术语 ID。无论哪种方式都很好,我只需要一个解决方案。这将用于 Drupal 7 zen 子主题

最佳答案

这个答案花费的时间比我预期的要长。困难的部分是收集节点上的术语,因为 All taxonomy functions relating to nodes have been removed or refactored .最终,Pro Drupal 7 Development 的第 355 页用一个片段挽救了这一天,该片段完成了之前由 taxonomy_node_get_terms 处理的工作。

下面是对我有用的代码(寻找写着“MAGIC BEGINS HERE”的部分)。假设您正在创建 Zen 的子主题,您需要将其移动到子主题的 template.php 文件中,并将其重命名为 YOURSUBTHEMENAME_preprocess_html:

/**
* Override or insert variables into the html template.
*
* @param $vars
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("html" in this case.)
*/
function zen_preprocess_html(&$vars, $hook) {
// If the user is silly and enables Zen as the theme, add some styles.
if ($GLOBALS['theme'] == 'zen') {
include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.zen.inc';
_zen_preprocess_html($vars, $hook);
}

// Classes for body element. Allows advanced theming based on context
// (home page, node of certain type, etc.)
if (!$vars['is_front']) {
// Add unique class for each page.
$path = drupal_get_path_alias($_GET['q']);
// Add unique class for each website section.
list($section, ) = explode('/', $path, 2);
if (arg(0) == 'node') {
if (arg(1) == 'add') {
$section = 'node-add';
}
elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
$section = 'node-' . arg(2);
}
// MAGIC BEGINS HERE
$node = node_load(arg(1));
$results = field_view_field('node', $node, 'field_tags', array('default'));
foreach ($results as $key => $result) {
if (is_numeric($key)) {
$vars['classes_array'][] = strtolower($result['#title']);
}
}
// MAGIC ENDS HERE
}
$vars['classes_array'][] = drupal_html_class('section-' . $section);
}
if (theme_get_setting('zen_wireframes')) {
$vars['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
}
// Store the menu item since it has some useful information.
$vars['menu_item'] = menu_get_item();
switch ($vars['menu_item']['page_callback']) {
case 'views_page':
// Is this a Views page?
$vars['classes_array'][] = 'page-views';
break;
case 'page_manager_page_execute':
case 'page_manager_node_view':
case 'page_manager_contact_site':
// Is this a Panels page?
$vars['classes_array'][] = 'page-panels';
break;
}
}

关于drupal - 如何将当前节点的分类法的主体类添加到 Drupal 7 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4642561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com