Theming nodes or placing CCK fields based on the viewed page

Last updated on
30 April 2025

Paging works on the node body, and by default all other CCK fields are visible on all pages. This behavior is not always what one may want - for example, we have an image, caption, credit, deckhead and whatnot, that needs to be viewed only on the (suppose) first page of the story.

Drupal 6

When using paging with a node, the node can have the fields themed individually using a preprocess function.

In your template.php file, create a THEME_preproess_node function, or add to it: (remember to replace YOURTHEME with the name of the theme you are using)

function YOURTHEME_preproess_node(&$vars) {
// Only do this if the paging module is enabled.
  if (module_exists('paging')) {
    // Get the page number.
    $page = isset($_GET['page']) ? $_GET['page'] : '';
    $pager_page_array = explode(',', $page);
    $current = isset($pager_page_array[1]) ? $pager_page_array[1] : 0;
    // Hide the image on any page other than 0.
    if ($current !== '0') {
      // Remove the rendered children.
      unset($vars['node']->content['#children']);
      // Set the whole content as unrendered (FALSE will not work here).
      $vars['node']->content['#printed'] = NULL;
      // Set the body as unrendered.
      $vars['node']->content['body']['#printed'] = NULL;
      // Set the pager as unrendered.
      $vars['node']->content['paging']['#printed'] = NULL;

      // Render the content again.
      $vars['content'] = drupal_render($vars['node']->content);
    }
  }
}

Drupal 5

## Code by yhager; taken from http://drupal.org/node/285862 ##

When using paging with a node, the node can have the fields themed individually in the template file.

In your template.php file, create _phptemplate_variables, or add to it:

function _phptemplate_variables($hook, $vars = array()) {
  if ($hook == 'node') {
    if (module_exists('paging')) {
      $page = isset($_GET['page']) ? $_GET['page'] : '';
      $pager_page_array = explode(',', $page);
      $vars['page'] = isset($pager_page_array[1]) ? $pager_page_array[1] : 0;
    }
  }
  return $vars;
}

Then, to show the image on the first page only, we have in node.tpl.php (or node-mycontenttype.tpl.php):

  <?php if (!$page): /* viewing the first page */?>
    <?php print $field_image[0]['view']; ?> 
  <?php endif; ?>
  <div class="body"><?php print $node->content['body']['#value'] ?></div>

Help improve this page

Page status: Not set

You can: