Related Articles - A Block View Of Nodes With Common Taxonomy Terms

test 提交于 周四, 06/27/2019 - 17:34

When you create content in your site, you really want to recommend additional content to your users, so that if you read this blog post, I'll be able to recommend another blog post I've written on a similar subject.

RELATED ARTICLES:

Let's start with what I think is the most common scenario when creating a blog post. You create a blog post, tag it with all the relavent tags, and you want to show in the sidebar additional blog posts that have similar tags.

for this we'll start with a plain new installation of Drupal 7, which comes loaded by default with the "Article" content type, as well as the the "Tags" Vocabulary and field on Articles.

Next, download, install and activate the Views, and Views_UI (bundled with views) modules.

Now, let's create some dummy articles:

  1. Title: Blog post about Drupal and Web Design, Tags: Drupal, Web
  2. Title: Blog post about Responsive Design, Tags: Drupal, Responsive
  3. Title: Blog post about Cats, Tags: Cats
  4. Title Blog post about Responsive Drupal Cats, Tags: Drupal, Cats, Responsive

Next, create a new view:

  1. Call it Related Articles
  2. Show: Content
  3. Type: Articles
  4. Create a page: Unchecked
  5. Create a blog: Check
  6. Continue & Edit
1

 

Ok, the first thing we need to do is access information about out Tags taxonomy terms on the article. Since taxonomy terms are stored on the article as reference field between the article node entity and the taxonomy term entity, we need to add a relationship between the node and the taxonomy based on the Tags field.

Click on the "Advanced" link to show the...well advanced options.

Add a Relationship, and search for the taxonomy term reference field name - Tags. Check the "Content: Tags (field_tags)" option, and click the "Apply (all displays)" button.

2

 

Click "Apply (all displays) again.

Now we can do the smart stuff, basically this is what we want to happend: when you're viewing an article, you want to show other articles that have the same tags in common with the current article. 

Currently our view will show all the articles (click "Update preview" at the bottom), so we want to filter out all the irrelavent articles.

Add a Contextual Filter, filter to "Taxonomy Term", and no check "Taxonomy term: Term ID", we want to filter the results according to Taxonomy term ids.

...Apply...

Views expect contextual filters to be given via the url, on a page with the url "blog", you'd pass a contextual filter like so: "blog/1". But we don't want to modify the url, we're just viewing an article right?

Under "When the filter value is NOT Available" select "Provide default value"

Next, under "Type" select "Taxonomy term ID from URL" (we're on an article, and we want to get their terms/tags from that article).

Now, here comes the real magic: Check " Load default filter from node page, that's good for related taxonomy blocks" , and "Limit Items by vocabulary", and "Tags" (this is optional, if you have multiple taxonomies on an article you don't have to limit the results).

Multiple-Value handling -> "Filter to items that share any term" - do we want only articles that share all the tags, on is just one enough.

3

 

Click on the "More" link at the bottom.

Check "Allow multiple values", I expect you'll want to use more than one tag on an Article, I know I do.

Let's save the view.

This view created a block for us called "Related Articles", lets place it in the sidebar.

5

 

Next, let's visit the first Article we created "Blog post about drupal and web design"

Ok, looks good but we have the same article multiple times...

6

 

Edit your view, and under "Other" -> "Query Settings" check the "Distinct" option.

 

7

 

Save your view and visit one of the articles we created.

Look! we have three articles in the sidebar... lets go to article number 3 (the cats one).

8

 

Here we have only two articles, the current one, and responsive drupal cats -> SUCCESS!

well, almost...why do we need the article we're looking at in the related articles block, of course it's related it's the same article.

Let's edit the view again.

So we want to filter out from the view the article we're looking at. Let's add another "Contextual Filter". This time, we want to filter out a particular article (node) so lets add "Content: Nid".

9

 

In the settings view, choose "Provide default value" again, but this time select "Content ID from URL" since want to grab the ID of the current article.

 

10

 

Click on the "more" link again, and check "Exclude"

We're excluding the id of the node that we're viewing.

11

 

Save, and preview.

 

参考视频:http://www.pixelwrapped.com/blog/related-articles-block-view-nodes-common-taxonomy-terms

标签
Article type

相关文章