2012-07-05 90 views
0

当前我使用this answer中建议的代码。这是以下几点:将类添加到Drupal 7中特定内容类型的图像字段中

function simalr_preprocess_image(&$variables) { 
    if ($variables['style_name'] == 'request-background') { 
    $variables['attributes']['class'][] = 'pixastic'; 
    $variables['attributes']['class'][] = 'pixastic-blurfast(amount=1)'; 
    } 
} 

这工作得很好,除了事实上,我得到它不以“请求后台”风格的图片页面上出现以下错误信息:

注意:simalr_preprocess_image()中的未定义索引:style_name(/var/www/vhosts/simalr.com/httpdocs/sites/all/themes/simalr/template.php第46行)。

我只想在特定内容类型(即'请求')上使用这段代码。在哪种方式下,我必须调整我的template.php文件中的代码,以便在只有特定内容类型的页面上使用它?

回答

0

您仍然可以使用您的代码,但使用isset函数。这将删除警告。

如果您希望仅为特定内容类型使用,请在drupal中使用menu_get_object函数。如果它是节点页面,此函数将为您返回节点。

例子:

$node = menu_get_object(); 

if ($node->type == 'story') { 
    // TODO 
} 

希望这有助于。

+0

谢谢,但我已经试过这个,但我仍然收到错误。你可能会重写我的代码给我一个更好的例子吗? – rroose 2012-07-11 17:43:05

相关问题