2010-05-20 19 views
5

我想給我的用戶提供改變頁面上使用的背景圖片的功能。根據用戶的選擇更改Drupal頁面的背景圖像...?

背景圖像的名單將是少數,不會真正改變。

我以爲我可以補充一些分類術語......爲每個背景類型...然後申請一類body標籤的瀏覽網頁時。

這聽起來可行,如果是的話我怎麼會去這樣做呢?

感謝

山姆

回答

1

編輯:澄清我的問題

的誤解後修訂答案如果背景圖像是要通過每(節點)頁面定義,你的方法分類詞彙聽起來像是正確的路要走。若要使CSS的條款,最簡單的方法是隻輸出/使用它們作爲node.tpl.php文件(S)班,在那裏你必須在$node變量的直接訪問。但是在這種情況下,它們有時會被掩埋在所產生的標記中,這使得使用它們有點困難。

爲了將它們添加到page.tpl.php中的$body_classes變量中,您不得不操作zen_preprocess_page()函數來添加它們,或者(更好的方法)將它們添加到您自己的模塊/主題中preprocess_page()功能,採用禪宗功能爲例:

function yourModuleOrTheme_preprocess_page(&$vars) { 
    // Add classes for body element based on node taxonomy 
    // Is this a node page? 
    if ('node' == arg(0) && is_numeric(arg(1))) { 
    // Yes, extract wanted taxonomy term(s) and add as additional class(es) 
    $node = node_load(arg(1)); 
    $background_vid = yourFuntionToGetTheBackgroundVocabularyId(); // Could be hardcoded, but better to define as variable 
    $terms = $node['taxonomy'][$background_vid]; 
    foreach ($terms as $tid => $term) { 
     // NOTE: The following assumes that the term names can be used directly as classes. 
     // You might want to safeguard this against e.g. spaces or other invalid characters first. 
     // Check the zen_id_safe() function for an example (or just use that, if zen is always available) 
     $vars['body_classes'] .= ' ' . $term; 
    } 
    } 
} 

注:未經測試的代碼,可能包含錯別字和其他疏漏。


原來的答覆之前編輯 - 基於對老年退休金計劃意圖的誤解,離開了她在其他情況下,它誤解以及:)
的基本想法聽起來是可行的,但我建議未成年人變化:

既然你想要的設置爲每用戶可調整的,你必須通過一些跳火圈,讓用戶「標籤」自己與一個分類項。我認爲只要啓用(核心,但是可選)profile module並在那裏配置一個「背景」字段(使用類型「列表選擇」)就會容易得多。該字段將顯示在用戶頁面上(或者在該頁面上的單獨標籤上,如果你給它一個類別),並且用戶選擇將很容易地從代碼中稍後獲得。爲頁面模板派生一個類:

global $user; 
// NOTE: The following call would be the explicit way, 
// but usually the profile fields get added to the $user object 
// automatically on user_load(), so you might not need to call it at all, 
// extracting the values directly from the $user object instead 
$profile = profile_load_profile($user); 
$background = $user->profile_background 
+0

這不是我設置的每個用戶的設置......它的目標是每頁。用戶在創建新頁面時將從列表框中選擇一種背景類型,並且所選背景對於該單獨頁面始終是相同的。 我正在使用Zen的子主題,它已經將'$ body_classes'輸出到所有頁面的主體標籤中。我想要做的是輸出分類術語以及此。 – Sambo 2010-05-24 03:47:00

+1

@Sambo:我明白了,對於誤會感到抱歉。我相應地編輯了我的答案。 – 2010-05-24 09:16:04

相關問題