Community Documentation

Theming nodes or placing CCK fields based on the viewed page

Last updated December 17, 2012. Created by Gurpartap Singh on January 24, 2009.
Edited by jenlampton. Log in to edit this page.

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)

<?php
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:

<?php
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>

Comments

Hello @all, how i can do this

Hello @all,

how i can do this in drupal 6. Have anybody a solution?

Kemal

I just used this in

I just used this in openpublish (D6.17 - should work on stock drupal too) using a subtheme called stv1_theme

in template.php create a function like:

<?php
function stv1_theme_preprocess_node (&$vars, $hook) {

    if (
module_exists('paging')) {
     
$page = isset($_GET['page']) ? $_GET['page'] : '';
     
$pager_page_array = explode(',', $page);
     
$vars['page'] = (int)isset($pager_page_array[1]) ? (int)$pager_page_array[1] : 0;
    };

}
?>

You will now have a variable called $page available to your node.tpl.php files which should indicate what page you're on.

for example you could put this in node.tpl.php (or in my case node-article.tpl.php)

<?php
if($page):
   
$main_image = NULL;
   
$field_deck = NULL;
endif;
?>

?>

Hope this helps.

doesn't work for me

I tried this for a site I'm working on, but when I added the code to my template.php file, I got the WSOD (white screen)

In my template.php file, I'm adding:

<?php
function pinnaclemag_preprocess_node(&$vars, $hook) {
      if (
module_exists('paging')) {
     
$page = isset($_GET['page']) ? $_GET['page'] : '';
     
$pager_page_array = explode(',', $page);
     
$vars['page'] = (int)isset($pager_page_array[1]) ? (int)$pager_page_array[1] : 0;
    };

}
?>

("pinnaclemag" is the name of my Omega sub-theme)

If I can get that part to work, then I'm adding the following to my node-pinnacle_article.tpl.php file:

<?php
if($page):
   
$field_subhead = NULL;
   
$field_pinnacle_image_1 = NULL;
   
$field_byline = NULL;
   
$field_article_images = NULL;
endif;
?>

Roger Goode | Artist/Designer | Hillsborough, NH

Hi All! Any variants how can

Hi All!
Any variants how can I do this in D7?

Thanks in advance.

Page status

About this page

Drupal version
Drupal 5.x, Drupal 6.x
Audience
Designers/themers
Level
Intermediate

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here