CCK字段过滤器修改

用CCK + imagefield + imageapi + imagecache 为内容类型为 article 创建了Image类型字段field_images,并设置上传数为无限,想实现用于发布文章时插入图片,由于字段field_images的图片无论多少都是同时出现或隐藏的,所以想在摘要(teaser)部分只显示第一张图片,而在全文(page)部分显示所有上传的图片。

步骤:

复制sites\all\modules\cck\theme\content-field.tpl.php 到主题目录sites\all\themes\test\,并复制重命名为content-field-field_images-article.tpl.php

这时主题目录sites\all\themes\test\ 多了2个文件:
content-field.tpl.php
content-field-field_images-article.tpl.php

修改content-field-field_images-article.tpl.php

<?php
// $Id: content-field.tpl.php,v 1.1.2.1 2008/06/19 01:59:53 yched Exp $

/**
 * @file field.tpl.php
 * Default theme implementation to display the value of a field.
 *
 * Available variables:
 * - $node: The node object.
 * - $field: The field array.
 * - $items: An array of values for each item in the field array.
 * - $teaser: Whether this is displayed as a teaser.
 * - $page: Whether this is displayed as a page.
 * - $field_name: The field name.
 * - $field_type: The field type.
 * - $field_name_css: The css-compatible field name.
 * - $field_type_css: The css-compatible field type.
 * - $label: The item label.
 * - $label_display: Position of label display, inline, above, or hidden.
 * - $field_empty: Whether the field has any valid value.
 *
 * Each $item in $items contains:
 * - 'view' - the themed view for that item
 *
 * @see template_preprocess_field()
 */

?>

<?php if (!$field_empty) : ?>
<?php if (!$page) { ?>
<?php print $items[0]['view'] ?>
<?php } else { ?>
<div class="field2 field-type-<?php print $field_type_css ?> field-<?php print $field_name_css ?>">
  <?php if ($label_display == 'above') : ?>
    <div class="field-label"><?php print t($label) ?>:&nbsp;</div>
  <?php endif;?>
  <div class="field-items">
    <?php foreach ($items as $delta => $item) :
            if (!$item['empty']) : ?>
              <div class="field-item">
                <?php if ($label_display == 'inline') { ?>
                  <div class="field-label-inline<?php print($delta ? '' : '-first')?>">
                    <?php print t($label) ?>:&nbsp;</div>
                <?php } ?>
                <?php print $item['view'] ?>
              </div>
      <?php endif;
          endforeach;?>
  </div>
</div>
<?php } ?>
<?php endif; ?>

主要增加

<?php if (!$page) { ?>

判断是全文还是摘要,然后打印第一个图片
<?php print $items[0]['view'] ?>

评论

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 允许HTML标签:<a> <img> <em> <del> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h5> <h6>
  • 自动断行和分段。
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

更多关於格式化选项的信息

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.