Search code examples
pluginsdocpad

Which code should I use to show related posts/docs on a docpad index-website?


Unfortunately I am no scripting-guru. So this might be obvious for the most of you.

I installed the docpad-plugin-related plugin and it works on individual posts/docs when I include the following code (see instructions)

Now, I would like to show posts/docs sorted by tag on my index-website. For example posts/docs tagged with webdesign should show up as a list. How do I have to change the following code for this purpose?

<ul>
<% for document in @document.relatedDocuments: %>
   <li><a href="<%= document.url %>"><%= document.title %></a></li>
<% end %>
</ul>

Is there a solution or does the plugin only work on individual posts/docs?


Solution

  • The docpad-plugin-related plugin is all about getting the related documents on an individual document which is not what you want. You don't need a plugin to do what you want.

    Assuming your documents all have a tags property, you can get a list of documents that match a specific tag (e.g. webdesign) with code on your index page like this:

    <ul>
    <% for doc in @getCollection('documents').findAll({tags: '$in': `webdesign`}).toJSON(): %>
        <li><a href="<%= doc.url %>"><%= doc.title %></a></li>
    <% end %>
    </ul>
    

    If you want tag specific indexes for all of your tags, you probably want to look at docpad-plugin-tagging (link).