2010-05-28 26 views
3
drupal_add_css('override/files/style.css'); 

這樣的說法不能確保CSS的最後加載,如何在drupal中添加最後加載的css?

我該怎麼做的伎倆?

+0

加載最後是什麼意思?最後一個CSS從一堆被加載?加載頁面底部的CSS?你是否在其他地方添加了其他的CSS文件? – acm 2010-05-28 11:24:58

回答

1

調用drupal_add_css()的順序添加了CSS,這很大程度上取決於執行調用的模塊或主題的權重,存儲在系統表中。 Drupal.org's instructions on changing module weight也適用於主題。

確定drupal_add_css()順序的另一個因素就是在Drupal的代碼中調用包含函數的順序。例如,template_preprocess_page()總是在template_preprocess_node()之後調用,只是因爲節點進入頁面。

+0

這聽起來不像是一個有效的解決方案。 – user198729 2010-05-28 13:38:17

0

如果你看看ninesixty主題的template.php主題文件,你會發現一個解決方案。

下面的函數從theme_preprocess_page鉤叫:

function ninesixty_css_reorder($css) { 
    global $theme_info, $base_theme_info; 

    // Dig into the framework .info data. 
    $framework = !empty($base_theme_info) ? $base_theme_info[0]->info : $theme_info->info; 

    // Pull framework styles from the themes .info file and place them above all stylesheets. 
    if (isset($framework['stylesheets'])) { 
    foreach ($framework['stylesheets'] as $media => $styles_from_960) { 
     // Setup framework group. 
     if (isset($css[$media])) { 
     $css[$media] = array_merge(array('framework' => array()), $css[$media]); 
     } 
     else { 
     $css[$media]['framework'] = array(); 
     } 
     foreach ($styles_from_960 as $style_from_960) { 
     // Force framework styles to come first. 
     if (strpos($style_from_960, 'framework') !== FALSE) { 
      $framework_shift = $style_from_960; 
      $remove_styles = array($style_from_960); 
      // Handle styles that may be overridden from sub-themes. 
      foreach ($css[$media]['theme'] as $style_from_var => $preprocess) { 
      if ($style_from_960 != $style_from_var && basename($style_from_960) == basename($style_from_var)) { 
       $framework_shift = $style_from_var; 
       $remove_styles[] = $style_from_var; 
       break; 
      } 
      } 
      $css[$media]['framework'][$framework_shift] = TRUE; 
      foreach ($remove_styles as $remove_style) { 
      unset($css[$media]['theme'][$remove_style]); 
      } 
     } 
     } 
    } 
    } 

    return $css; 
} 
0

使用權重屬性:

drupal_add_css(drupal_get_path('theme', 'mytheme') . '/css/styles.css', array('weight' => 999)); 

注:CSS在你的主題的信息加到將始終是最後一個,所以使用相同的像以前一樣改變在主題的信息文件中聲明的樣式表的權重。

1

爲Drupal 6用途:

drupal_add_css('site.css', 'theme', '', array('weight' => 999)) 
+0

(我知道這是舊的,只爲其他讀者)對於Drupal 7也。 – 2015-05-21 16:56:22

4

使用組屬性:

drupal_add_css(drupal_get_path('theme', 'mytheme') . '/css/styles.css', array('group' => CSS_THEME); 
0

這爲我工作。在頁面上的最後一個css之後添加css

$module_path = drupal_get_path('module', 'my_module'); 
    drupal_add_css($module_path . '/css/my_module.css', array('weight' => 99999, 'group'=>CSS_THEME));