Filtering views with dynamic node type

I felt really sorry when i realized that you can not filter a view with dynamic node types. And now it's possible with a few tricks.

Lets say you have three content types: news, blog and articles. Also you have a block to show teasers of some nodes and you use a view called content_teasers. At the detail page of a node, you want  to show only same content types of teasers at that block but views module only gives you "node type filter" with options news, blog and articles, so it's not possible to make it dynamic.

Here is my trick:

1. Download views php filter here install it.

2. Prepare your view and name it content_teasers, decide what field to use etc.

3. At the filter section use node: node id and paste this code:

 

  1. global $current_view;
  2. $type = $current_view->args[0];
  3.  
  4. if($type =="")
  5. {
  6. $type= "blog"; //if nothing found, show blog teasers
  7. }
  8.  
  9. $nids = db_result(db_query("SELECT GROUP_CONCAT(node.nid) FROM node WHERE node.type = '%s' ", $type));
  10.  
  11. return $nids;

4. As you can see, i used a global variable $current_view and i got the content type from it's arguments. There must be somewhere to set it obviously. Open your page.tpl.php whatever theme you are using.

5. Paste this code whereever you want to show the block

  1. global $current_view;
  2. $current_view->args[0]=$node->type;
  3. $view1 = views_get_view('content_teasers'); // this is the name of your view
  4. print (views_build_view('block', $view1, $current_view->args, false, 3)); // more info here

 

6. Go to phptemplate>phptemplate.engine and find function phptemplate_regions() in the core. Add another region here, lets say content_teaser_block.

7. Administer>site building>blocks, match content_teasers with content_teaser_block.

That's it, now when a visitor click to a content, he will see the same content type teasers of other nodes.

 

You can download the code from below

EkBoyut
code.rar411 byte

Yeni yorum gönder

Bu alanın içeriği gizli tutulacak ve açıkta gösterilmeyecektir.
 .