Drupal Views:获取与当前节点具有相同分类的节点
我有一个带有分类术语的内容类型。它是一个选择列表,因此它只能有一种分类法 - 至少是该词汇表。现在我需要构建一个视图,列出具有相同分类的所有节点。
我认为这不会太难,因为它听起来很基本。但我无法让它发挥作用。有什么想法吗?我试图避免为此编写模块。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然这在技术上可以通过 Views 2 实现,如其他一些答案中所述,但 Views 3 内置了此集成。
您可以为分类术语 id 创建参数,然后选择“提供默认参数” 。然后,这将为您提供一个选项“来自 URL 的分类术语 ID”和“从节点页面加载默认参数...”
这将允许您采用以下分类一个页面并将其作为参数传递给您的视图块。
注意:Views 3 目前处于 Alpha 3 阶段,但根据我的经验,它处于相对稳定的状态,并且我正在生产站点上使用它。如果它具有您认为有用的上述功能,请使用它、测试它并在遇到任何问题时提交错误/补丁!
While this was technically possible with Views 2 as described in some of the other answers, Views 3 has this integration built in.
You can create an argument for the taxonomy term id and then choose "Provide default argument". This will then give you an option for "Taxonomy Term ID from URL" and "Load default argument from node page..."
This will allow you to take the taxonomy of a page and pass that as an argument to your view block.
Note: Views 3 is currently in Alpha 3 but in my experience is at a relatively stable state and I am using it on production sites. If it has features like the one above that you find useful please use it, test it and submit bugs/patches if you encounter any issues!
此答案适用于 Views 版本 2 或更高版本。首先,您需要安装 Views Attach ( http://drupal.org/project/views_attach )。在继续操作之前,请先阅读有关视图附加的内容。这个视频可以很好地解释视图附加 http://mustardseedmedia.com/podcast/episode37
现在我们得到附加到视图。请先启用视图附加模块,然后再继续。本质上,
Views Attach
在节点末尾附加一个视图。在我们的例子中,我们的视图将是具有相同术语的其他文章的列表。我们本质上需要将节点的分类术语“传递”到视图。将您的词汇表名称命名为
MyVocab
。制作视图的步骤。
display_other_articles_with_same_taxonomy
。节点内容
类型的显示(启用视图附加后可用)。这是一种类似于块和页面显示的显示,但具有将自身附加到节点的特殊能力。在
节点内容显示
中进行以下设置。您应该在参数下选择
使用视图附加到的节点中的令牌
。让标记为[term-id]
这是“顶级分类术语的ID”。这非常重要!本质上,您将从MyVocab
传递节点的分类术语(请参阅 http://groups.drupal.org/node/11788#comment-38332)。如果它的权重最低,则分类词汇将是节点编辑表单的分类部分中的第一个词汇。添加参数分类:术语 ID
。添加您感兴趣的字段,例如
Node: Title
。确保通过勾选将此字段链接到其节点
将节点:标题
制成超链接。因此,此视图要做的是:
display_other_articles_with_same_taxonomy
就是这样!
如果您使用的是 Views 3(在撰写本文时目前处于 alpha3 状态)并且您想要一个块(现在这些文章在节点主体末尾有相同的分类术语)您可以这样做按照以下方式:
节点内容
显示说明相同的参数、字段和过滤器。Taxononomy:Term Id
的设置:在如果参数不存在则采取的操作:
下选择[x]提供默认参数
。选择[x]来自 URL 的分类术语 ID
。现在确保取消选择[]从术语页面加载默认参数
并且[x]从节点页面加载默认参数,这对于相关分类块很有用。
另外[ x]按词汇表限制术语
并选择系列
词汇表。注意:这个答案是我在 如何在包含的节点上显示 NodeQueue 块? 在该场景中,要求是显式选择相关文章并按特定顺序列出。它有点复杂,并且使用我们在这里根本不需要的 Nodequeues。
This answer works in Views version 2 or higher. First you need to install Views attach ( http://drupal.org/project/views_attach ). Please read about Views attach before proceeding further. Views attach is best explained by this video http://mustardseedmedia.com/podcast/episode37
Now we get to Views attach. Please enable the views attach module before proceeding. Essentially
Views attach
attaches a view at the end of the node. In our case our view will be a listing of other articles with the same term.We will essentially need to "pass" the taxonomy term of the node to the view. Let the name of your Vocabulary be called
MyVocab
.Steps to make the view.
display_other_articles_with_same_taxonomy
.Node Content
(available after enabling Views attach). This is a display just like block and page displays but with special ability of attaching itself to the node.Make the following settings in the
Node Content Display
You should select
Use tokens from the node the view is attached to
under Arguments. Let the token be[term-id]
This is the "ID of top taxonomy term". This is very important!! Essentially you are going to be passing the taxonomy term of the node from theMyVocab
(See http://groups.drupal.org/node/11788#comment-38332). If it has the lowest weight, the taxonomy vocabulary will be the first vocabulary in the taxonomy section of your node edit form. Add an argumentTaxonomy: Term Id
.Add the fields you are interested in e.g.
Node: Title
. Make sure theNode: Title
is made into a hyperlink by tickingLink this field to its Node
So what this view is going to do is:
MyVocab
vocabulary in the Node that is currently being vieweddisplay_other_articles_with_same_taxonomy
Thats it!
If you're using Views 3 (currently at alpha3 at the time of writing) and you want a block (right now the articles have same taxonomy term come at the end of node body) you can do it in the following fashion:
Node Content
display.Taxononomy: Term Id
just slightly: UnderAction to take if argument is not present:
choose[x] Provide Default Argument
. Choose[x] Taxonomy Term ID from URL
. Now make sure[] Load default argument from term page
is unselected and[x] Load default argument from node page, thats good for related taxonomy blocks.
Also[x]Limit terms by vocabulary
and choose theSeries
vocabulary.Note: This answer is subset of the answer I provided at How to just show NodeQueue block on included nodes? In that scenario the requirement was that the related articles are explicitly selected and listed in a particular order. Its a little more complex and uses Nodequeues which we don't need here at all.
使用关系
Use relationships
视图为您提供添加过滤器的选项。单击视图管理 UI 的过滤器区域中的加号,从列表中选择分类法,选中术语或 ID,然后填写您需要作为过滤依据的值。
编辑(用于明确说明):
首先,在要从尝试过滤的节点中显示的字段部分下添加所需的必要字段,例如节点标题等。
检查分类法:术语并单击添加
单击更新
在预览区域中通过添加进行测试参数并单击预览。如果它不起作用,则您的节点没有附加分类法,您的视图模块已损坏,或者您没有遵循指示。
Views gives you the options to add filters. Click the plus sign in the filters area of the views admin UI, select Taxonomy from the list, check either Term or ID, and fill in the value that you need to filter by.
EDIT (for explicit instructions):
First, add the necessary fields that you want under the fields section that you want to display from the nodes that you are trying to filter such as node title, etc.
Under Arguments, click the plus sign and select Taxonomy
Check Taxonomy: Term and click Add
Click Update
In your preview area test it out by adding an argument and clicking preview. If it doesn't work, your nodes do not have attached taxonomy, your views module is corrupted, or you didn't follow directions.