By kayman-1 on
Is there a way to display the last x nodes using a different node template, like node-columns.tpl.php, based on taxonomy with a list limit? I'm trying to include it on page.tpl.php.
The following (working) code displays the last 3 items from taxonomy id 11.
<?php
$listlength = 3; // [maximum] number of titles to show.
$taxo_id = 11; // the desired term ID.
$res = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n INNER JOIN term_node ON n.nid = term_node.nid WHERE term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC"), 0, $listlength);
$output .= theme('node', $anode, $teaser = FALSE, $page = FALSE);
print $output;
?>
The following modified code did not work with the defined node template:
<?php
$listlength = 3; // [maximum] number of titles to show.
$taxo_id = 11; // the desired term ID.
$res = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n INNER JOIN term_node ON n.nid = term_node.nid WHERE term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC"), 0, $listlength);
$output .= theme('node', $anode, $teaser = FALSE, $page = FALSE);
include 'node-columns.tpl.php'; /*load node-columns.tpl.php */
return;
print $output;
?>
I also tried modifying node.tpl.php to load a different template, but it did not work either. I'm sure it's because the taxo id isn't usable.
<?php
if ($t->tid == '12') {/* check if it's taxonomy id 12 */
include 'node-columns.tpl.php'; /*load node-columns.tpl.php */
return; }
include 'node-default.tpl.php';
return;
?>
Pointers, snippets, a solution.. Any help would be greatly appreciated :)
- K
Comments
you're making it too hard
here is where you create your output:
you don't need to pass each node through theme('node') if you are (essentially) applying a known, custom theme function. I assume you do not have multiple themes on your site?
Put your code (or register a callback and use a template file) in template.php with a name like format_node-columns().
As a starting point for format_node-columns(), see (for example) theme_node()
---
Work: BioRAFT
Thanks, but I'm screwing it up.
Thanks so very much for your help. You're right, I'm not using multiple themes. I had seen the topic you listed but I didn't see the correlation until you mentioned it. However (ugh), I still can't get it to work. I posted the three bits of code below that I tried after reading your post (again and again :) ). If you can spare some more time... where oh where am I blowing this?
page.tpl.php
template.php
node_columns.tpl.php
A couple changes and a trick
You code is missing one important part, it never loops through the records returns so anode is never set. Also you want the whole node so in the query we will return on the nid and load the whole node in the loop. Also you want to call your theming function through the theme function.
Now this will get you closer, but your template is using variables ($node_url, $title, $content) only set up for node.tpl.php, so you can change your hook to set the variables, use values directly from $node or maybe this trick will help (I have not tried it). Just after the call to node_load, replace the call to theme(...) with
and change your template file to node-columns.tpl.php.
use actual name of function
(note edited in response to simultaneous comment above)
I think you're 95% there.
To start, you need to iterate though the DB results:
You're only showing teasers, right? Never a full node? So, I think in template.php, change it to:
I don't think there is any reason to go through theme(), so you just call the function directly by name (an alternative approach to the one above).
In node_columns.tpl.php, note that you only have access to $node, not the usual predefined variables. That's why I suggested you look at theme_node().
You want something more like (note, I haven't actually tried any of this code, and see also the l function reference):
let me know if it works!
---
Work: BioRAFT
Display last x by taxonomy w/diff node.tpl.php in 4.7.2
I love you guys :)
We have a winner! This totally worked for me.
Hopefully this is useful for others and the subject I used will help with people's searches..
Thank you both so much for your help. You both opened my eyes to Drupal-things I haven't yet grasped.
Here's the final product:
In page.tpl.php
In node_columns.tpl.php:
In template.php:
I'll let you know if it blows up on me in some way..
- K
$name?
Does-
work?
---
Work: BioRAFT
Of course not.
Of course not. That's just another stupid mistake on my part when I posted that. :(
This is what I actually ended up using.
Thanks again,
- K
on edit: for some reason, the css attribute (below) isn't working for me:
Probably something I'm just not seeing right now.
Thanks again and G'nite.