2011-06-15 83 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