I have found the code snippet in the documentation on how to make a block visible based on the taxonomy vocabulary and or term of the node being viewed. It works great...

However now I need the block to dynamic display the content based on the terms of the node. But can't seem to figure it out. To clarify, in case that didn't make sense, I want to make a single block (using views) to display content using the same term of the node being viewed. When someone visits a different node the block updates the displays the new content with the same term (of the node). The content type in the block already uses the same terms as the page content.

Thank you for the help! And I will pre-apologize if this turns out to be easy and already answered somewhere else.

Comments

WorldFallz’s picture

To create a block view that displays nodes that share ANY term in common with the current node:

  1. create the block view as desired
  2. add an argument of "Taxonomy: Term ID"
  3. select 'provide default argument' and 'php code'
  4. enter the following into the code box:
    if (arg(0) == 'node' && is_numeric(arg(1))) {
      $node=node_load(arg(1));
      return implode('+', array_keys($node->taxonomy));
    } else {
      return FALSE;
    }
    

    If you want only the nodes with ALL the same terms change the '+' to ','

  5. check the 'Allow multiple terms per argument' option

To remove the current node from the list:

  1. add an argument of "Node: Nid"
  2. select 'provide default argument' and 'php code'
  3. enter the following into the code box:
    if (arg(0) == 'node' && is_numeric(arg(1))){
      return arg(1);
    } else {
      return FALSE;
    }
    
  4. check the 'Exclude the argument' option
jeremyr’s picture

Thanks! That's working out great for me.

I'm a little lost on the second part. Is that snippit to prevent a specific node from displaying with the block?

W: jprasmussen.com

WorldFallz’s picture

It's to prevent the currently viewed node from also appearing in the list in the block. Depending on the other options you use in the view (like sort order) the currently viewed node may be listed. That argument will exclude it. If you don't care that it gets listed twice, just omit it.

jeremyr’s picture

Oh, I have other filters to prevent that. They are actually two different node types.

Since you've been so helpful perhaps you can help me with another. I have the block displaying as a list and linking them back to the node. Is there a way to add a class to the links?

I see there is a "Rewrite the output of this field" but I'm not certain if this is where something like that would happen.

W: jprasmussen.com

WorldFallz’s picture

You can do it by theming the view for sure, but where is the "rewrite the output of this field" option-- i'm not familiar with it?

Also, have you examined the views html output? There's plenty of classes to style things already there-- you may not need one.

jeremyr’s picture

I am actually trying to insert a specific class into the link because I'm trying to use the popup api (not views popup) which requires class="popup" inside of the A tag.

I'm looking into the theme templates to see if can't create a custom one. Thanks for all your help!

W: jprasmussen.com

WorldFallz’s picture

ah ok-- you might be able to do this with a tiny bit of jquery-- just create the appropriate selector(s) for the items you want to have the popup class and use the jquery addClass function. Probably easier than theming a view just for that reason.

i'm still curious to know where the 'rewrite' option is...

jeremyr’s picture

I'll have to give that a try, it sounds like an easier fix.

I've linked an image to show you where the rewrite option is. I've already added a Field (Node: Title) and when it gives me the options at the bottom before I commit the changes this is the screen I see.

http://img3.imageshack.us/my.php?image=viewsrewriteoutputxw3.jpg

W: jprasmussen.com

WorldFallz’s picture

Thanks for the pic-- at least now I know I'm not crazy. I don't have that option-- I think it was added in v2.3 and i'm on still on 2.2

jeremyr’s picture

Figured out a better way to do this and thought you'd like to know (plus anyone else who happens to search this topic.

You know that Rewrite check box that I showed you in the image? Well, I checked that box and added this in the text box for the Node Title Field:

<a href="/node/[nid]" title="[title]" class="popups">[title]</a>

and it works great, no extra modules or templates needed. Basically all you have to do is make sure everything that you want to use in the Rewrite is called before the field that you are rewriting. This makes them available to use in this text box. So, I added Node ID to the view and then made sure that the check box "Exclude from display" was checked and then used it's pattern in the second view [nid] for the rewrite.

Make sense? Anyway, I'm happy I figured it out.

W: jprasmussen.com

WorldFallz’s picture

Very slick-- thanks for posting back. Guess I'm going to have to upgrade my views and play around. ;-)

OneTwoTait’s picture

Didn't work at all for me...

WorldFallz’s picture

Also relevant to this thread are the following modules which make a more concerted effort and identifying related content than available with views: