Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
19 posts Page 1 of 2
Hi all,

I need to design a blog site for a customer, who needs to have his articles classified with by using tags/keywords/key-terms (folders won't do). Is there an easy, Couch-like way to implement taxonomies like this?

Many thanks in advance for any guidance, or tips.
Hi,

We can use 'relations' to create the 'tags' functionality (to be precise, a 'fixed tagging' functionality).

As a concrete example, suppose we have a blog.php template and we need to tag the individual posts.
For this, use a clonable template, say named 'tags.php' -
Code: Select all
<cms:template title='Blog Tags' clonable='1' ></cms:template>

Relate the blog template with it by adding the following region to blog.php -
Code: Select all
<cms:editable name='tags' type='relation' masterpage='tags.php' label='Tags'  />

Now, the user can create all the tags terms as cloned pages of tags.php.
While creating a blog post, the tags will be shown in a listbox with multiple checkboxes.

On the front-end, we can use the following code to show the tags a blog post is related to -
Code: Select all
<!-- blog.php - showing tags related to a post -->
<cms:capture into='my_tags'>
    <cms:related_pages 'tags' >
        <cms:show my_tags /><cms:if k_count gt '1'>,</cms:if> <a href="<cms:show k_page_link />"><cms:show k_page_title /></a>
    </cms:related_pages>
</cms:capture>

<cms:if my_tags ><cms:show my_tags /></cms:if>


Each of the tag shown above leads to the tags.php template's page_view where we can display all the blog posts related to that particular term
Code: Select all
<!--  tags.php - showing pages related to the tag (in page-view of a tag) -->
<cms:set my_custom_field_str="tags=<cms:show k_page_name />" />
<cms:pages masterpage='blog.php' limit='10' custom_field="<cms:show my_custom_field_str />">
   ... all data for blog posts ...
</cms:pages>

Finally, we can also create a tag-cloud like this -
Code: Select all
<!-- show tags that have atleast one post associated -->
<div class="tags"> <strong class="stitle"> Tags</strong>
    <ul>
    <cms:pages masterpage='tags.php' custom_field="blog.php::tags=ANY"  >
        <li><a href="<cms:show k_page_link />"><cms:show k_page_title /></a></li>
    </cms:pages>
    </ul>
</div>

<!-- same as above but with count display -->
<div class="tags"> <strong class="stitle"> Tags</strong>
    <ul>
    <cms:pages masterpage='tags.php' 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>
</div>

IMP: All code above uses Couch v1.4.5RC1 (http://www.couchcms.com/forum/viewtopic.php?f=5&t=8581) and above.

Hope it helps.

Addendum:
For the use of 'tags' to filter pages listing, please see a variation of this technique in the following thread -
http://www.couchcms.com/forum/viewtopic ... 43&p=19515
Many thanks, KK - it certainly helps a lot!

This entry should be filed in the 'Tips & Tricks' section!
Done :)
Hi, i have for some time now used couch and im very happy about it. But now i need this TAG function and when i try i get ERROR CANT FIND MASTERPAGE:TAGS.PHP

What do i do wrong. I follow the steps you explained here.

Please help, maybe explain a bit more detail.

Thanks in advance
kimheggen, welcome!

Did you create tags.php template? If yes, make sure you visited it, while logged as superadmin. It then should be present in admin panel.
edit: besides first steps, you must have relations enabled and established in your template.
Hi,

Thanks for this. I got a bit to work. So now i see the tags in CMS admin, and i can add tags and i can se the tags on my lising post in blog.php

But when i click one tag i get empty page, nothing will show. Using following code:
Code: Select all
<?php require_once( 'couch/cms.php' ); 
include ( 'header.php' );
?>

<cms:template title='Blog Tags' clonable='1' >
   
    <cms:set my_custom_field_str="tags=<cms:show k_page_name />" />
<cms:pages masterpage='blog.php' limit='10' custom_field="<cms:show my_custom_field_str />">
   ... all data for blog posts ...
</cms:pages>
   
</cms:template>

<?php
include ( 'footer.php' );
COUCH::invoke();
?>
Hi,

The code that does the listing needs to be moved out of the <cms:template> block. Please try the following -
Code: Select all
<?php require_once( 'couch/cms.php' );
include ( 'header.php' );
?>

<cms:template title='Blog Tags' clonable='1' >

</cms:template>

<cms:set my_custom_field_str="tags=<cms:show k_page_name />" />
<cms:pages masterpage='blog.php' limit='10' custom_field="<cms:show my_custom_field_str />">
... all data for blog posts ...
</cms:pages>

<?php
include ( 'footer.php' );
COUCH::invoke();
?>

Hope it helps.
Thanks.

How can i get the posts related to spessific tag get listed on the tags page?

I have in bottom of all posts a list of the selected tags and when i click on one of them i want all post tagged with the clicked tag name get listed on a page. (tags.php)


Thanks for all help :)
The solution discussed in this thread (i.e. your code) does exactly that, @kimheggen.
Could you please let us know what seems to be different in your code?
19 posts Page 1 of 2