2011-06-15 79 views
7

我正在用Drupal 7.2构建一个站点,并使用了几个有用的模块(视图,显示套件和20多个)。
我在内容类型(文章)中添加了一个图像字段,并将'字段设置' - >'数量值'设置为5,这意味着用户可以为该字段上传5个图像。
在'Full Content'查看模式下,我想显示所有图像,但是如何在'Teaser'模式下只显示一个图像?有没有任何模块可以做到这一点?如何在预览模式下显示ONE图像,同时在完整内容下显示多张图像?

回答

9

我加入了对这一领域的类型像领域的一个新的模板解决了这个问题 - 使用下面的代码field_image.tpl.php在我的情况:

// Reduce image array to single image in teaser view mode 
if ($element['#view_mode'] == 'teaser') { 
    $items = array(reset($items)); 
} 

print render($items); 

希望这有助于。

编辑:这里的(可能)正确的方式做到这一点:

function MYTHEME_process_field(&$vars) { 
    $element = $vars['element']; 

    // Field type image 
    if ($element['#field_type'] == 'image') { 

    // Reduce number of images in teaser view mode to single image 
    if ($element['#view_mode'] == 'teaser') { 
     $item = reset($vars['items']); 
     $vars['items'] = array($item); 
    } 

    } 

} 
+0

〜它的工作原理已经回答了,谢谢!〜让我们拭目以待,看看是否有更好的解决方案〜 – MessyCS 2011-06-15 11:29:43

+0

大。一般来说,我猜想使用预处理钩子会更优雅。 – bjo3rnf 2011-06-15 11:32:09

+0

是的,这就是drupal的精神 – MessyCS 2011-06-15 11:39:54

2

它没有工作对我来说,我不得不调整sligthly代码:

function MYTHEME_process_field(&$vars) { 
    $element = $vars['element']; 

    // Reduce number of images in teaser view mode to single image 
    if ($element['#view_mode'] == 'node_teaser' && $element['#field_type'] == 'image') { 
    $vars['items'] = array($vars['items'][0]); 
    } 
} 
1
function MYTHEME_process_field(&$vars) { 
    $element = $vars['element']; 

    // Reduce number of images in teaser view mode to single image 
    if ($element['#view_mode'] == 'teaser' && $element['#field_type'] == 'image') { 
    $vars['items'] = array($vars['items'][0]); 
    } 
} 

我已将node_teaser更改为teaser,它对我有用。 Drupal v 7.19 P.S.不要忘记刷新缓存。

2

一个选项无需更改代码是使用模块Field Multiple Limit。使用此模块,您可以选择在允许多个条目的字段的哪个视图中显示多少个项目。

然后在字段的预告视图中,您可以选择要显示或跳过的项目数。

刚看到这是在Drupal Stack Exchange

+0

Field Multiple Limit为我做了诀窍。 – albertski 2016-01-05 14:51:28