Hi there.
I have created the Nodetype "Animals". Furthermore there is a voacabulary "Species" with taxonomy terms. This vocabulary is used by all content types.
What i want is, when you watch a node from the type "Animals" that there is a block which shows all nodes which belongs to the same taxonomy term.
I dont want to create a new view for every term. i think its a question of arguments. hope you can help me!
Comments
Comment #1
smacphail commentedYup, it will come down to using arguements. I haven't been using Views very long, and I'm an intermediate PHP developer, (so pros, please chime in and correct/improve my suggestion) but this is the solution that worked me:
1. you'll basically want to set up an argument on Term ID (or Term Name).
2. Under "Action to take...", choose "Provide default argument"
3. Under "Default argument type", use "PHP Code"
4. Views doesn't know about your $node, which is where your taxonomy term for "Species" is set, so you need to load $node.
menu_get_object()in this case is the equivalent of goingnode_load(args[1]). I believe it's a new function to D6.Now that you've got your $node object, you'll need to loop through its taxonomy. (NOTE: this assumes that your custom content type "Animals" has only *one* vocabulary assigned to it ("Species") and that the vocabulary can only have one value.)
That will return the very first term id in your taxonomy object, so then Views will then look for Animals that are tagged with the same species.
If you have a few different vocabularies assigned to your content type, either make sure Species is the "heaviest" vocabulary (that means its value will always be first, above the others in the taxonomy array), or you'll have to get Species vocabulary ID first (
taxonomy_get_vocabularies();), and then loop through the entire taxonomy object, looking to see where the taxonomy term you're looking at has a vocabulary ID that matches up with Species.So, the full version of this whole argument would look something like this:
Yea, it's a little long, but I'm not sure just yet how it can be done any other way/improved.
BTW, if you *also* want to make sure that you don't load in the Animal that you're currently looking at into your View, set up another argument for Node ID, do the same thing (Provide default, PHP code, etc.) and the argument will be
That will return the ID of your node. Then scroll to the bottom of the Views argument options and make sure to "Exclude" this item from the result set.
Now your Views will show other Animals with the same Species and will exclude the one you're currently looking at. Unless I made a mistake above. :P
Good luck!
Comment #2
Witch commentedHi smacphail!
I solved this issue by this code
thank you for trying to help me!
Comment #3
smacphail commentedExcellent! Glad you were able to sort it out. The only "problem" is that your vocabulary id hard coded to "4", but if you never expect that to change or you're not going to move to another server or whatever, then you're fine. It saves you one less query of the database.
EDIT - I made a mistake in my code, with the statement where I'm looping through the vocabularies.
I didn't define $vocab_name anywhere, so you should just change that to
== "Species"or whatever.