【问题标题】:Drupal image disappearsDrupal 图像消失
【发布时间】:2014-11-18 11:07:21
【问题描述】:

我的页面顶部的图片有问题。 当url 是网站名称/菜单时,会显示image。 当url 为网站名称/节点/164 时,image 消失。

所以image在level为1的时候会显示,但是当url-level高于1的时候image就消失了。

我在Drupal-7 上使用ZURB-Foundation 主题。 这是我在page.tpl.php中使用的代码

<!--.page -->
<div role="document" class="page">
<?php
// Create a variable to hold the full path, in our theme, to the image.
//   path_to_theme() takes care of creating the correct path for the active theme (which is  likely your own custom one)
$my_static_banners = path_to_theme() . '/images/header/';
?>
<?php
if ($handle = opendir(path_to_theme() . '/images/header')) {
/* This is the correct way to loop over the directory. */

while (false !== ($picture = readdir($handle))) {
  if($picture != "." && $picture != ".."){
    $pictures[] = $picture;
  }
}
$random = array_rand($pictures,1);
$header_picture = $pictures[$random];
closedir($handle);
}
?>
<!-- MENU-->
<!--.l-header region -->
<header role="banner" class="l-header" >
<div class="header-image" style="background-image:url(<?php print     $my_static_banners.$header_picture ?>)">
</div>
</div>
...

【问题讨论】:

    标签: drupal drupal-7 zurb-foundation


    【解决方案1】:

    请尝试减少主题层中的 php 代码量。

    在您的页面模板中:

    <div role="document" class="page">
    <!-- MENU-->
    <!--.l-header region -->
    <header role="banner" class="l-header" >
      <div class="header-image" style="background-image:url(<?php print $header_image; ?>);">
      </div>
      </div>
    

    在主题中的 template.php 或自定义模块中:

    /**
     * Implements hook_preprocess_page().
     */
    function THEME_NAME_preprocess_page(&$variables) {
      $variables['header_image'] = FALSE;
      $images = _THEME_NAME_get_images();
      if ($images) {
        $random = array_rand($images, 1);
        $variables['header_image'] = $images[$random];
      }
    }
    
    /**
     * Get images within a particular director.
     *
     * @return array
     *   An array of images for the header.
     */
    function _THEME_NAME_get_images() {
      $dir = drupal_get_path('theme', 'THEME_NAME') . '/images/header';
      $images = array();
      if (is_dir($dir) && $handle = opendir($dir)) {
        while (FALSE !== ($filename = readdir($handle))) {
          if ($filename[0] != '.') {
            $images[] = $dir . $filename;
          }
        }
        closedir($handle);
      }
      return $images;
    }
    

    【讨论】:

    • 我得到一个错误:注意:未定义的变量:include() 中的 header_image(C:\wamp\www\raedthuijs\sites\all\themes\foundation_raedthuijs\templates\page.tpl 的第 27 行。 php)
    • 当没有调用预处理时会发生这种情况,请确保为您的钩子替换 THEME_NAME,无论是您放置此代码的主题名称还是模块“命名空间”。一旦你有这个清除你的缓存。尝试放一个 var_dump($variables);死();在 hook_preprocess_page() 实现中,一旦你的页面死掉,你就知道钩子正在被实现。
    • 谢谢,这行得通,我不知道我首先做错了什么,但现在可以了。我认为它现在有效,因为我清除了缓存。但是当我转到 /node/290 之类的 url 时,图片仍然消失。当我检查代码时,正确的 url 被放置在代码中,但没有图像出现
    • 你能发一份html输出的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多