Community Documentation

Theming nodes or placing CCK fields based on the viewed page

Last updated January 24, 2009. Created by Gurpartap Singh on January 24, 2009.
Log in to edit this page.

## Code needs to be updated so that it works for Drupal 6 too -- Gurpartap Singh ##
## Code by yhager; taken from http://drupal.org/node/285862 ##

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

Paging works on the node content, and all CCK fields are visible on all pages (except for the node body that is being paged). 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.

Here's what can be done to implement that:

In 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

Page status

Needs updating

Log in to edit this page

About this page

Drupal version
Drupal 5.x, Drupal 6.x

Site Building Guide

Drupal’s online documentation is © 2000-2012 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.