Skip to main content

不使用404 block模組,也能顯示左右區域的內容

Posted in

之前,曾經介紹過使用404 Blocks模組,讓左右兩區域能在404 page not found頁面中也能顯示出來

今天,我們將介紹如何能夠不使用此模組也能達到同樣的效果- 參考至:Showing left and right region on page not found

其實很簡單,我們只要將以下的程式加到template.php(這個可以到您版型的目錄下找到)中。

function phptemplate_preprocess_page(&$variables) {
  // show all regions if page not found
  $page_not_found = strpos(drupal_get_headers(), 'HTTP/1.1 404 Not Found') !== FALSE;
  if (!$variables['show_blocks'] && $page_not_found) {
    global $theme;
    // Populate all block regions.
    $regions = system_region_list($theme);
    // Load all region content assigned via blocks.
    foreach (array_keys($regions) as $region) {
      // Prevent left and right regions from rendering blocks when 'show_blocks' == FALSE.
      if ($region == 'left' || $region == 'right') {
        $blocks = theme('blocks', $region);
      }
      // Assign region to a region variable.
      isset($variables[$region]) ? $variables[$region] .= $blocks : $variables[$region] = $blocks;
    }
  }
}

請您注意:

  1. 剛剛的程式中有個變數"$variables"需要請您注意與您版型中原本的變數是否相同。
  2. 如果您的版型中原本就已經有了"function phptemplate_preprocess_page"(garland版型中是已經存在的),那麼您只需要添加以下的內容到"phptemplate_preprocess_page"函數中,即可。而不能另外在新增一個function phptemplate_preprocess_page-這會產生錯誤訊息的。

  //以garland版型為例
  // show all regions if page not found
  $page_not_found = strpos(drupal_get_headers(), 'HTTP/1.1 404 Not Found') !== FALSE;
  if (!$vars['show_blocks'] && $page_not_found) {
    global $theme;
    // Populate all block regions.
    $regions = system_region_list($theme);
    // Load all region content assigned via blocks.
    foreach (array_keys($regions) as $region) {
      // Prevent left and right regions from rendering blocks when 'show_blocks' == FALSE.
      if ($region == 'left' || $region == 'right') {
        $blocks = theme('blocks', $region);
      }
      // Assign region to a region variable.
      isset($vars[$region]) ? $vars[$region] .= $blocks : $vars[$region] = $blocks;
    }
  }

舉例來說,在Drupal原始版型"garland"中,變數名稱為"$vars", 您就需要將所有的"$variables"改為"$vars"

修改完成,由於我們變動了template.php,因此請您到管理>網站建置>版型(admin/build/themes) 的頁面下,重新加載版型,讓Drupal重新的載入版型

這樣就可以了。試試看吧!

No votes yet

發表新回應

這個欄位的內容會保密,不會公開顯示。
  • 自動將網址與電子郵件位址轉變為連結。
  • 可使用的 HTML 標籤:<img> <a> <p> <br /> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><h1> <h2> <h3>
  • 自動斷行和分段。

更多關於格式選項的資訊

CAPTCHA
為了網站安全,請您輸入正確答案,謝謝。
Image CAPTCHA
Enter the characters (without spaces) shown in the image.