【问题标题】:Drupal 7: calling custom content type field into page tplDrupal 7:将自定义内容类型字段调用到页面 tpl
【发布时间】:2011-03-04 15:37:58
【问题描述】:

我正在开发一个 Drupal 7 网站。我需要一些页面的自定义布局。所以我创建了 page--customContentTypeName.tpl.php 文件,它完美地解决了问题。

问题是,我需要在页面 tpl 中显示一些字段。下面的代码在节点 tpl 中运行良好,但是页面 tpl :/

<?php print $content['field_images']['#items']['0']['filename']; ?>" />

如何将自定义字段调用到页面 tpl?

感谢帮助!非常感谢!!


** 排序 **

自定义字段编辑...这里是教程视频:http://lin-clark.com/blog/intro-drupal-7-theming-fields-and-nodes-templates#comment-54

【问题讨论】:

    标签: drupal cck drupal-7 drupal-theming


    【解决方案1】:

    对于 page.tpl.php 如果你直接访问节点,你可以使用 $node 变量

    $node['field_images']['und'][0]['filename']
    

    否则使用 $page 变量。

    $page['content']['system_main']['nodes'][1]['field_images']['#items'][0]['filename'];
    

    但请记住,在一个页面变量中,您可能有多个节点。

    【讨论】:

      【解决方案2】:

      7改变了结构,字段先按语言键(“und”默认为“未定义”),然后你可以按照这个例子:

      // Array of Image values
      $images = $node->field_images['und'];
      
      //If you don't know the language, the value is stored in:
      $node->language
      
      
      // First image
      $image = $images[0];
      
      
      
      // If you need informations about the file itself (e.g. image resolution):
      image_get_info( $image["filename"] );
      
      
      // If you want to access the image, use the URI instead of the filename !
      $public_filename = file_create_url( $image["uri"] );
      
      
      // Either output the IMG tag directly,
      $html = '<img src="'.$public_filename.'"/>';
      
      // either use the built-in theme function.
      $html = theme(
          "image",
          array(
              "path" => $public_filename,
              "title" => $image["title"]
          )
      );
      

      注意使用uri 而不是filename 将图像嵌入页面,因为File API in Drupal 7 is more abstracted(以便更容易与CDN 服务集成)。

      【讨论】:

      • 另请注意,在预告模式下(例如,列出许多节点的分类术语页面),默认情况下变量 $node 中不提供此类字段。
      • 顺便说一下,StackOverflow 的 Drupal 专区将在几天后开放公测:drupal.stackexchange.com
      【解决方案3】:

      drupal 中有 2 个对主题开发人员有用的模块: 开发和主题_developer 开发模块提供了一个名为 dsm() 的函数。 使用 dsm 您可以识别元素是如何存储在不同对象中的。 像节点或... 例如,您可以使用以下语句:dsm($node) 页面中任何节点的结构都将显示在消息框中。 您可以在代码中键入语句。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-11
        • 1970-01-01
        • 2012-09-03
        • 2011-05-20
        • 2020-04-03
        • 2021-04-23
        相关资源
        最近更新 更多