Search code examples
phpdrupal-7content-typerelated-content

Displaying related content on a page


AnyI'm working with Drupal 7 and I'm trying to match to related nodes based on the taxonomy value that they have been tagged with. I want to achieve this without using views or blocks. So I have two content types. Content type 1 is the main page and content type 2 acts like a right hand block which sits on a content type 1 article.

.......................
|     C1               |                   
|          _________
|          |  C2   |   |
           |       |   |
|          |_______|   |
                       |
|                      |
.......................

So the illustration above demonstrates how the page will look like. My question is: How can I pull in the right C2 to show on C1 based on taxonomy tagging. So if C1 is tagged with dog how can I show a C2 that's also tagged with dog? I know this post is a bit vague. Any suggestions would be much appreciated.


Solution

  • If you don't want to use views, then you need to write your custom module to get the related content, for that many ways are available i will give one option step by step,

    1. Add a taxonomy reference filed in C1.
    2. Add taxonomy reference field to the C2 with same taxonomy vocabulary.
    3. Now on your node page you will get the tags associated with that C1 node,
    4. Query in C2 taxonomy field table with the tid's associated with C1,
    5. Get the entity_id from that table to get the node id's related to your current node.