[英]Add class to an image field for a specific content type in Drupal 7
Currently I'm using the code as suggested in this answer . 目前,我正在使用此答案中建议的代码。 Which is the following:
如下:
function simalr_preprocess_image(&$variables) {
if ($variables['style_name'] == 'request-background') {
$variables['attributes']['class'][] = 'pixastic';
$variables['attributes']['class'][] = 'pixastic-blurfast(amount=1)';
}
}
This works fine except for the fact that I get the following error message on a page which doesn't have an Image with the 'request-background' style: 除了我在没有“请求背景”样式的图像的页面上收到以下错误消息外,此方法运行正常:
Notice: Undefined index: style_name in simalr_preprocess_image() (line 46 of /var/www/vhosts/simalr.com/httpdocs/sites/all/themes/simalr/template.php).
注意:未定义的索引:simalr_preprocess_image()中的style_name(/var/www/vhosts/simalr.com/httpdocs/sites/all/themes/simalr/template.php的第46行)。
I only want this piece of code used on a specific content type (namely 'request'). 我只希望将这段代码用于特定的内容类型(即“请求”)。 In which way do I have to adjust the code in my template.php file in order to just use it on a page which is only of a certain content type?
为了只在特定内容类型的页面上使用它,我必须以哪种方式调整template.php文件中的代码?
You can still work with your code but use isset
function. 您仍然可以使用代码,但使用
isset
函数。 This will remove the warning. 这将删除警告。
If you want to do it just for a specific content type use menu_get_object
function in drupal. 如果只想针对特定的内容类型执行此操作,请在drupal中使用
menu_get_object
函数。 This function will return the node for you if it's a node page. 如果它是节点页面,此函数将为您返回该节点。
Example: 例:
$node = menu_get_object();
if ($node->type == 'story') {
// TODO
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.