试图将分类法术语从视图插入Drupal 7站点的页面标题


Trying to insert taxonomy term from view into page title on Drupal 7 site

我的网站上有许多视图使用分类法术语作为上下文过滤器。我正试着把那个分类学术语放到页面标题中。

'Latest TAXONOMY_TERM News | Sitename etc.'

我在template.php中的THEME_views_post_render函数中使用taxonomy_term_load来获取术语并构建页面标题。然后,我将这个变量传递给THEME_preprocess_html,在那里我使用它来覆盖$variables['head_title']。

views_post_render:

$term = taxonomy_term_load($view->result[0]->_field_data['nid']['entity']->field_term['und'][0]['tid']);
$page_title = 'Latest ' . $term->name . ' News | ' . variable_get('site_name', 'Sitename etc');

preprocess_html:

$variables['head_title'] = $page_title ;

如果我回显$variables['head_title'],我可以看到我在THEME_views_post_render中定义的页面标题,但它显然缺少分类术语。

'Latest News | Sitename etc.'
有谁能解释一下这里发生了什么吗?我确实考虑过在THEME_preprocess_html函数中执行所有逻辑,但这似乎无法访问视图。

感谢您的帮助。

Rob。

在视图标题中,您可以使用替换模式,它将显示term-name。使用它,您可以设置所需的页面标题。

您没有提供如何在代码片段中计算/填充$view变量。首先要确保你做对了。我还建议使用Devel模块的dpm功能(或它的朋友),而不是使用echo进行调试。