Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By sumancool.jb on
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
Arguments/Contextual filter
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:
You can also do this with the other code:
You can pass as many arguments as your view has contextual filters
Have a doubt left
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.
Instead of $node->nid, I want to keep it as $nid.
Please help me out. Thanks in advance.
I'm not sure what your
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?
Its not working
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 ?
View with Contextual Filter?
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.
What I intend to do
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.
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.
Is there any option to pass this $nid to the above code and display the view for a single content.
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 ??..
Contextual Filter
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...
Thank you Spovlot
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.