Show block on specific pages via PHP Snippet
jdc32 - May 18, 2008 - 11:57
Hi there,
i would show a Block at every Node with a specific Taxonomy.
Is there any Array with all Taxonomy Words from a Node, so i can use it at the Block configuration "Show block on specific pages via PHP"
Thanks so far.
Melwin

Try something like <?php
Try something like
<?php$myterm = "display_block_if_node_matches_this_term";
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$terms = taxonomy_node_get_terms(node_load(arg(1)));
foreach($terms as $term) {
if ($term->name == $myterm) return TRUE;
}
}
?>
gpk
----
www.alexoria.co.uk
just show 1 block of information
If a node has two taxonomy what code do i have to add to the snippet to only show 1 block.
I use this code to show different blocks:
block 1:
<?php$myterms = array(22); // list the ids of the terms you want
// This will show on all nodes having this term
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$terms = taxonomy_node_get_terms(arg(1));
foreach($terms as $term) {
if (in_array($term->tid, $myterms)) return TRUE;
}
}
// This will show on the index page for that term
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (in_array(arg(2), $myterms))) {
return TRUE;
}
// Otherwise
return FALSE;
?>
block 2:
<?php$myterms = array(21); // list the ids of the terms you want
// This will show on all nodes having this term
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$terms = taxonomy_node_get_terms(arg(1));
foreach($terms as $term) {
if (in_array($term->tid, $myterms)) return TRUE;
}
}
// This will show on the index page for that term
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (in_array(arg(2), $myterms))) {
return TRUE;
}
// Otherwise
return FALSE;
?>
So if a node matches both terms i just want to show 1 block.
Thnx,
Peter
I assume you are using
I assume you are using Drupal 5, in which http://api.drupal.org/api/function/taxonomy_node_get_terms/5 expects a node id as the first argument, not a complete node.
If on a node matching both terms you want to display block 1 in preference to block 2 you could do:
block 2:
<?php$myterms = array(21); // list the ids of the terms you want
$notterms = array(22); // list the ids of the terms you mustn't match
// This will show on all nodes having any of the terms you want but none of the terms you don't want
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$terms = taxonomy_node_get_terms(arg(1));
$show = FALSE;
foreach($terms as $term) {
if (in_array($term->tid, $notterms)) return FALSE; // definitely don't show block on this page
if (in_array($term->tid, $myterms)) $show = TRUE; // show the block on this page, provided we don't match anything in $notterms
}
// If we reach this line of code then $show contains the answer we want
return $show;
}
// This will show on the index page for that term
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (in_array(arg(2), $myterms))) {
return TRUE;
}
// Otherwise
return FALSE;
?>
gpk
----
www.alexoria.co.uk
Dependancy
It's a little different, sorry for my bad explanation.
I created nodes which belong to 1 or more taxonomies and there are 6 taxonomies.
So if a node is belonging to 2 or more taxonomies then i only want to display 1.
The website is : www.werkenbijdimence.nl
(I also have a problem that with node type filter that if i filter on a taxonomy it won't show the block.)
Yes i am using Drupal 5 for this.
Thanx for your reply,
Peter
I think you might be best to
I think you might be best to use Views module since to do what you want via PHP will require a bit of customizing of the code depending on what your exact requirements are. With Views you should be able to create a "view" that will just pick out the first relevant taxonomy term (or by sorting on a specific criterion you can probably control which term is deemed to be "first". Views 1 will only return node(s), so you would need to put your block content in different nodes, and then tell Views to output your node in a block (provide the view as a block). So you might want to create a custom content type for the nodes which will contain the "block" content, tag each with the relevant single term, then create your View to pick out the node from your custom content type with matching term and output it to a block.
This may sound complicated but then what you are wanting is I think not entirely trivial (there are a few ifs and buts, more than I think you've described so far..!). If you have further queries I suggest you open a new forum topic or post an issue to the Views issue queue with category = support request.
gpk
----
www.alexoria.co.uk