Hi guys,

I am looking for displaying a view using php code for a particular node id. I have found this code working.

$view = views_get_view('my_view');
print $view->preview('default');

This code is also working

print views_embed_view('my_view', 'default', $node->nid);

I need to show the view for a particular node id. The codes above displays the whole block. Can I know how to show a particular content with a particular node id( for example node id=4).

Expecting a little help as I am stuck here for a while.

Comments

spovlot’s picture

Your view needs to have a contextual filter for the Content: Nid . The contextual filter is the same as a view argument.

In this case, you are passing the $node variables nid as an argument:

print views_embed_view('my_view', 'default', $node->nid); 

You can also do this with the other code:

$view = views_get_view('my_view');
print $view->preview('default',$node-nid);

You can pass as many arguments as your view has contextual filters

print views_embed_view('my_view', 'default', $node->nid, $arg2, $arg3); 
sumancool.jb’s picture

Thanx for the reply. But my issue is not yet solved. The node id is passed along with the link (nid=4). In the required page, I get it using the php code ($_GET['nid']). Now I need to display the view for this particular id.

print views_embed_view('my_view', 'default', $node->nid);


Instead of $node->nid, I want to keep it as $nid.

$nid = $_GET['nid'];

Please help me out. Thanks in advance.

spovlot’s picture

I'm not sure what your question is exactly. Also, I am unsure what your context for this code is. Are you using it in a hook or a custom module? What URL are you using?

Did you try this?

$nid = $_GET['nid'];
print views_embed_view('my_view', 'default', $nid);
sumancool.jb’s picture

I had tried it. Its not working. Its displaying the whole contents in the view. I want a single content to be displayed with the node id $nid.

In my case I have three contents in the view with id's 3,4 and 5. I wanted to display just one. I passed id=4. But that didn't wok. Instead, its showing all the three contents.

Any idea ?

spovlot’s picture

Does your view have a contextual filter on Content: Nid ? Does your view work in Preview mode when using the node id?

Also, you still have not presented your complete or explained how or where you are using it.

sumancool.jb’s picture

Normally, I used to show a view in the required pages wherever I want and then used to have a Node: Link (a link to the original content displayed in the page.tpl.php). Now I intend to connect this link to another view of same content type in another page. What I have done so far is this,

1. created 2 blocks(views) of the same content type. (my_view1 & my_view2)

2. In the first view (my_view1), I added a Content: Nid. Excluded this field from display.

3. Then I added a Global: Custom text. Under Rewrite results, I used "Output this field as a link" option. Set Link path as pagename?nid=[nid] . I got [nid] from Replacement Pattern.

4. Now I am getting the redirection link correctly (Eg : /pagename?nid=4).

5. I picked the id in the redirected page using basic php code.

$nid = $_GET['nid']; 


What I need now is

Now I need to display the second view, my_view2 for a single content. The content id is there in the page, $nid. I need a code to display it. This is all I need to do.

print views_embed_view('my_view2', 'default', $node->nid);


Is there any option to pass this $nid to the above code and display the view for a single content.

$view = views_get_view('my_view2');
print $view->preview('default', $args);


The above method passes the argument to the view. I don't know how to pass the nodeid as the argument. I think its passed as an array. I am not sure about this. But I hope so.

Do you have any solution for my issue ??..

spovlot’s picture

You still have not answered my first question. Does your view have a Contextual Filter defined? You can find Contextual Filters under advanced. Arguments will not work unless a contextual filter is defined.

For more info, see http://nodeone.se/en/learn-views-with-nodeone-part-19-contextual-filters...

sumancool.jb’s picture

Thank you dear..

There was a lot of trouble in understanding what you meant. But I have done it now. The problem was my reluctance to understand the concept of Contextual filters. I just placed an argument by filtering nid in the contextual filters.

Thank you a lot for the help. Just can't literally express it.