target first teaser in taxonomy/term page

jmburnz - May 9, 2008 - 20:47

ok, quick question, my brain is dead right now...

how might I target the first teaser in a teaser list, in order to style it differently?

I am using views - taxonomy_term view

Since it's a view you can

nevets - May 9, 2008 - 22:05

Since it's a view you can theme it, in this case you keep track if it the first node and add a flag to $node after node_load and before node_view, like this (the code would be added to your themes template.php file, if your viewed is named something other than 'taxonomy_term' replace taxonomy_term in the function name with your views name.

function phptemplate_views_view_nodes_taxonomy_term($view, $nodes, $type, $teasers = false, $links = true) {
  $first = TRUE;
  foreach ($nodes as $n) {
    $node = node_load($n->nid);
    $node->first_in_list = $first;
    $output .= node_view($node, $teasers, false, $links);
    $first = FALSE;
  }
  return $output;
}

Now in your node.tpl.php file you could use $node->first_in_list to add a class to your outer div.

Hey, thanks nevets, that

jmburnz - May 10, 2008 - 10:27

Hey, thanks nevets, that should help me out a lot, very much appreciated.

For anyone who does not

jmburnz - May 11, 2008 - 21:39

For anyone who does not fully follow what nevets is saying, here is what I ended up using in my node.tpl.php file - this is the outer div code...

<div class="node" id="<?php if ($node->first_in_list) : print "node-first"; endif; ?>">

Its safe to use an id rather than a class since there is only one of them.

I have a update to this

jmburnz - May 12, 2008 - 18:21

I have a update to this question that has been put to me...

How might we pull a single field from the first node (from the taxonomy term teaser list) and print it elsewhere, specifically in the header as a meta tag (desc).

These are CCK types with a "field_teaser", and its important to 1) not pull the title or links etc, just the field_teaser, and 2) that's its plain text with no markup at all and 3) able to print it in the header (which ever way).

We're using a unique template for taxonomy pages (page-taxonomy.tpl.php).

One possible approach

nevets - May 12, 2008 - 21:13

Since the page template file where you print the header does not "know" about either the list or the first node in the list I would resort to a global, starting with something like this in template.php (modified version of theme function from above)

global $first_node_teaser;
function phptemplate_views_view_nodes_taxonomy_term($view, $nodes, $type, $teasers = false, $links = true) {
  global $first_node_teaser;
  $first = TRUE;
  foreach ($nodes as $n) {
    $node = node_load($n->nid);
    if ( $first ) {
       // You will want to strip any html
       $first_node_teaser = check_plain($node->field_teaser[0]['value']);
    }
    $node->first_in_list = $first;
    $output .= node_view($node, $teasers, false, $links);
    $first = FALSE;
  }
  return $output;
}

Then in page-taxonomy.tpl.php you can declare and print $first_node_teaser.

Could you clarify a bit

jmburnz - May 12, 2008 - 22:39

Could you clarify a bit about what's going on here, perhaps if I can understand this a bit more I can get it to work.

Why is the variable $first_node_teaser declared as global in both the global and local scope?

How do you mean "declare and print $first_node_teaser" in page-taxonomy - isn't $first_node_teaser already declared i.e.

$first_node_teaser = check_plain($node->field_teaser[0]['value']);

Sorry, just trying to understand how this works and I am not entirely sure how to declare and print the variable in the page tpl.

You need to declare the

nevets - May 12, 2008 - 23:09

You need to declare the variable at global scope (the first one) and in ever function you reference it.

In the page tpl you would want something like

<?php
global $first_node_teaser;
print
$first_node_teaser;
?>

You need to declare it global because it is not in the same scope as template.php.

Ok, I fully understand this

jmburnz - May 13, 2008 - 00:57

Ok, I fully understand this now, very good - I had to make a few small changes such as...

// added strip_tags to get rid of link code etc, check plain wasn't handling that and it all converted to html entities
$first_node_teaser = check_plain(strip_tags($node->field_teaser['0']['value']));

wrote a small function to convert remaining entities to normal characters, probably not necessary, but some were malformed for some reason so its just a function to strip out or convert any crap that remains, eg...

<?php

 
global $first_node_teaser;
  print
$first_node_teaser = replace_html_entities($first_node_teaser);
?>

 
 

Drupal is a registered trademark of Dries Buytaert.