Display (x) recent nodes (with teasers) of a specified type from a specified taxonomy term
Since I am not professional in php this code might need to be revised by more skillful people. What is important - it works fine with the nodes of a custom (flexinode) content type. Just change 'story' content type into 'flexinode-1' or something like that...
<?php
/**
* This php snippet displays content of a specified type, with teasers,
* from certain taxonomy term.
* Sorted by date of creation, most recent first.
* Works with nodes of the flexinode type too.
* To change the length of the list, change $listlength.
* To change the taxomony term, change $taxo_id.
* To change the type of content listed, change the $content_type.
* Tested with Drupal 4.6.3
*/
$listlength="5";
$taxo_id = 1;
$content_type = 'story';
$result1 = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC"), $listlength);
while ($node = db_fetch_object($result1)) {
$output .= node_view(node_load(array('nid' => $node->nid)), 1);
}
print $output;
?>
Hack job on above code
This php solution isn't perfect, but I needed to strip out all the html in stories on Hogmanay.net so that I had short, very brief intro to a story in a specified taxonomy appearing on the front page. Drupal's shortest teaser at 200 words is too long for my requirements. Also, didn't want big images and links that appear at the start of the document to appear on the home page. Hence the use of striptags()
The PHP strips out all the HTML and then posts the first x amount of characters as text (where x is $charlength), with a link to the story.
I'm sure there is a better way. Suggestions welcome ):)
<?php$listlength="1";
$charlength="150";
$taxo_id = 1;
$content_type = 'story';
unset ($output);
$result1 = pager_query("SELECT n.title, n.nid, n.teaser, n.uid, n.created, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC", $listlength);
while ($node = db_fetch_object($result1)) {
$output .= l($node->title, "node/$node->nid") . "<br>" . substr(strip_tags($node->teaser), 0, $charlength) . "... <em>Submitted by " . $node->name . "</em>";
}
$output .= "<br>";
print $output;
?>
Here is what I'm doing...
First off thanks for starting me off in the right direction, now to the point!
I've taken the above code and first placed it in a page using the add content page module, that worked.
I then removed it and placed the script in my template under the comment ""
I was able to set the template to display only the top 3 stories from that category. Exactly what I wanted, but there is a problem, the links stop working I think because the templates page file was modified.
So I then tried to add an "If" statement to react to the "q" query string (code below). I'm still testing but the idea is if "q" is empty then show the home page style layout, else, if "q" is not empty build the page as if you were viewing a story in a page module. Any help would be sooo Cool.
<?php
$pageChk = $_GET[""];
if(empty($pageChk)){
$listlength="3";
$charlength="200";
$taxo_id = 1;
$content_type = 'page';
unset ($output);
$result1 = pager_query("SELECT n.title, n.nid, n.teaser, n.uid, n.created, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC", $listlength);
while ($node = db_fetch_object($result1)) {
$output .= "<div class='pagetitle'><h2>" . l($node->title, "node/$node->nid") . "</h2></div>" . substr(strip_tags($node->teaser), 0, $charlength) . "... <br /><br /><em>Submitted by " . $node->name . "</em>";
}
$output .= "<br />";
print $output;
} else {
print($content);
}
?>
Display specific taxonomy based on page template file
Ok the code below allows you to modify a page.tpl.php template file so that it only shows a specific category for a given template. Note I'm using a PHP template and mainly focused on the Page.Module functionality being that all of the sites just need to show news articles.
The goal of this adventure was to have one data base with multiple websites. All the websites have to have a different look and feel but all the sites will share the same database. Now the kicker was that each site should show content (page module type) that has a specific taxonomy term, i.e. website1.com should only show articles with the term labled website1 and so on.
So here is what I did;
First set the settings file to reflect the use of a template other than the default-
(The code below is from files in the follow order, settings.php, page.tpl.php)
(settings.php from /sites/site1.com/)
<?php
/* Start By Setting The Base URL */
/**
* Base URL:
*
* The URL of your website's main page. It is not allowed to have
* a trailing slash; Drupal will add it for you.
*/
$base_url = 'http://localhost.site1.com'; /*Local website example that I used, the folder in the sites directory is named sites1.com*/
/* settings file stuff not shown for space reasons, nothing modified here move down to Variable Overrides in the file*/
/* Set Variable Overrides By Uncommenting the lines below in your settings.php file */
/**
* Variable overrides:
*
* To override specific entries in the 'variable' table for this site,
* set them here. You usually don't need to use this feature. This is
* useful in a configuration file for a vhost or directory, rather than
* the default settings.php. Any configuration setting from the 'variable'
* table can be given a new value.
*/
$conf = array(
'site_name' => 'Site Number 1',
'theme_default' => 'kubrick', /*folder name of the theme you want to use loacted in the main theme folder at the root of drupal*/
'anonymous' => 'Visitor'
);
?>
(page.tpl.php from /themes/kubrick/)
<?php
/* Now go to the theme folder and click the folder of the theme you've choosen in the settings.php file and open the page.tpl.php file and find the comment <!-- start main content -->
Replace what is there with this code below, oh make back ups first! */
?>
<?php
if (@$_REQUEST["q"] == "") {
$listlength="3";
$charlength="200";
$taxo_id = 1;
$content_type = 'page';
unset ($output);
$result1 = pager_query("SELECT n.title, n.nid, n.teaser, n.uid, n.created, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC", $listlength);
while ($node = db_fetch_object($result1)) {
$output .= "<div class='pagetitle'><h2>" . l($node->title, "node/$node->nid") . "</h2></div>" . substr(strip_tags($node->teaser), 0, $charlength) . "... <br /><br /><em>Submitted On " . $node->name . "</em>";
}
$output .= "<br />";
print $output;
} else {
print($content);
}
?>
That's it! You can change the content type to what you are using, you can change the number of items displayed on the home page and you can specify what term ID should be shown as well as how long the teaser should be, enjoy and let's make this better!
port for 4.7
I modified code for 4.7 like these...
<?php$listlength="1";
$charlength="150";
$taxo_id = 1;
$content_type = 'story';
unset ($output);
$result1 = pager_query("SELECT n.title, n.nid, nr.teaser, n.uid, n.created, u.name FROM {node} n join {node_revisions} nr on (n.nid = nr.nid and n.vid = nr.vid) INNER JOIN {users} u ON n.uid = u.uid INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC", $listlength);
while ($node = db_fetch_object($result1)) {
$output .= l($node->title, "node/$node->nid") . "<br>" . substr(strip_tags($node->teaser), 0, $charlength) ."...";
}
$output .= "<br>";
print $output;
?>
now im looking for the way to strip img_assist code too ...no luck so far
www.ferjan.net