Problems, need help? Have a tip or advice? Post it here.
10 posts Page 1 of 1
Let me explain my issue as best as I can:
I have a blog.php file with two fields with type relation:

Code: Select all
<cms:template title='Blog Posts' clonable="1">
...
  <cms:editable name='categories'
    label='Categories'
    masterpage='categories.php'
    has='one'
    type='relation'
    order='3' 
  />

  <cms:editable name='tags'
    label='Tags'
    masterpage='tags.php'
    type='relation'
    order='5' 
  />
...
</cms:template>


The tags and category files are like this:

Code: Select all
<cms:template title='Categories' clonable='1' >
  <cms:config_list_view exclude='default-page'>
  </cms:config_list_view>
</cms:template>

<cms:embed 'categories.html' />

A blog post can have one category and multiple tags associated with it.

I have a page at /tags, where I am successfully able to show all the tags of the site. But I was wondering if there is a way to show the tags associated with specific categories on the /tags page. I would like to do this on the /tags page, as well as for each category landing page.

Any suggestions are welcome, and thank you in advance for your time.
Hi Karen,

My apologies but the use-case is not clear to me from the description -
could you please elaborate on the following a bit more please?
But I was wondering if there is a way to show the tags associated with specific categories on the /tags page. I would like to do this on the /tags page, as well as for each category landing page.

Some concrete examples would be very helpful to illustrate the points.
Let's say I have 3 categories: Food, Sports, Travel. Each blog post can only be associated with one category. However, I can add multiple tags to each blog post. Right now, the Categories and Tags are two clonable templates associated with the Blog.

On the tags page, I would like to list the tags by category. So, for example, on the Tags landing page:

Food
#indianFood #burgers #restaurantReviews

Sports
#football, #IPL, #baseball

Travel
#backpacking #keralaTourism

And on the individual Category landing pages, e.g. /categories/food, /categories/travel, I would like to list only the tags that were added to posts that belong to that specific category.
Thanks, it is clear now :)

The tags and categories templates do not have an explicit relation (being only related indirectly through blog pages) -
therefore it would require a custom query to create the kind of listing you desire efficiently (efficient being the keyword here).

No problem in doing that but I noticed from the examples that you quoted that each tag seems to be implicitly related to a certain category -
Food
#indianFood #burgers #restaurantReviews

Sports
#football, #IPL, #baseball

Travel
#backpacking #keralaTourism

I.e., #baseball will never likely be used for any category other than sport.
If that is correct, then an easier way of handling this use-case would be to define a relation field in tags.php linking it to categories. Any time you create a tag, just select the correct category for it.

This way doing the listing (in both the templates) would be a simple matter of listing related pages.
Do you think this would be feasible? Please let me know.
KK wrote: Any time you create a tag, just select the correct category for it.

:) It is a good Prototype decision that removes costly queries.

Perfect solution, imo, as it would allow to add only Tags to cloned Pages and remove Categories relation altogether as often those are well mixed in posts (Travel and Sports are very close, as well as Sports and Food, Travel and Food), thus a page may become attributed to different categories solely based on its Tags. E.g. if a page has 2 tags from Food and 1 tag from Sports, it can be displayed in both categories, which boost total page views (=good for ads).

In short, page's category(ies) will not be defined by Admin, but will be auto-determined based on page's tags.
In an ideal case, I would not like to restrict the tags to belonging to a specific category (which my example did not indicate). I understand this may be a bit of an expensive operation in the database, but since the site is small and articles would rarely cross 100 in the next few years, I'm wondering if that could be done, even if the query is not too efficient. If not, I can apply the simpler option that KK mentioned.
@imohkay
I set out to create the custom query and quickly realised that that this use-case can be handled using the stock Couch <cms:pages> tag alone -
Enhanced cms:pages tag (viewtopic.php?f=5&t=8581)
Tags and Taxonomies (viewtopic.php?f=8&t=8952)

Please try using the following in your tags home-view:
Code: Select all
<!-- loop through all categories -->
<cms:pages masterpage='categories.php'>
    <h2><cms:show k_page_title /></h2>

    <!-- first find blog pages related to the current category -->
    <cms:set my_blog_pages="<cms:pages masterpage='blog.php' custom_field="categories=<cms:show k_page_name />" ids_only='1' />" />
   
    <!-- then find tags related to the pages found above -->
    <ul>
        <cms:pages masterpage='tags.php' custom_field="blog.php::tags=id(<cms:show my_blog_pages />)" aggregate_by="blog.php::tags" >
            <li><a href="<cms:show k_page_link />"><cms:show k_page_title />  (<cms:show k_rel_count />)</a></li>
        </cms:pages>
    </ul>
</cms:pages>

For individual Category pages, remove the outer <cms:pages masterpage='categories.php'> loop from the code above and use only the enclosed contents.

Hope this helps. Please let us know.
Hey KK,
I tried it out and it works perfectly! This is great and I'm guessing will come in handy for future Couch projects. Thank you so much :)
You are welcome :) I am glad it helped.
Letting the db engine handle duplicate IDs is smart :) :)
10 posts Page 1 of 1