In Part 1 we have learned how to assign multiple keywords (tags) to entries of CGBlog Module. Now I want to show you how to retrieve these tags in order to make them visible and clickable in the frontend and browse our entries by them.
We need a new “Summary Template” in CGBlog Module. Call it as you want like “Tag-Summary” and insert the following code (major code is just copied from sample):
[php]
<!– Start CGBlog Display Template –>
{if isset($error)}{cgerror}{$error}{/cgerror}{/if}
{if $pagecount > 1}
{if $pagenumber > 1}
{$firstpage} {$prevpage}
{/if}
{$pagetext} {$pagenumber} {$oftext} {$pagecount}
{if $pagenumber < $pagecount}
{$nextpage} {$lastpage}
{/if}
</p>
{/if}
{foreach from=$items item=entry}
{if !empty($smarty.get.tag) && ($entry->fieldsbyname.Tags->value|strpos:"`$smarty.get.tag`" !== false)}
<div class="summary">
{if $entry->postdate} <div class="date">{$entry->postdate|cms_date_format}</div>{/if}
<div class="title"><a href="{$entry->detail_url}">{$entry->title|escape}</a></div>
{if $entry->categories}
<div class="category">
{strip}{$category_label}
{foreach from=$entry->categories item=’category’}
{$category.name}
{/foreach}
{/strip}
</div>
{/if}
{if $entry->author}<div class="author">{$author_label} {$entry->author}</div>{/if}
{if $entry->summary}
<div class="summary">{eval var=$entry->summary}</div>
{else if $entry->content}
<div class="content">{eval var=$entry->content}</div>
{/if}
{if isset($entry->extra)}
<div class="CGBlogSummaryExtra">
{eval var=$entry->extra}
{* {cms_module module=’Uploads’ mode=’simpleurl’ upload_id=$entry->extravalue} *}
</div>
{/if}
{if isset($entry->fields)}
{foreach from=$entry->fields item=’field’}
<div class="CGBlogSummaryField">
{if $field->type == ‘file’}
<img src="{$entry->file_location}/{$field->value}"/>
{elseif $field->name == ‘Tags’}
<h5>Tags</h5>
{assign var="taglist" value=$entry->fieldsbyname.Tags->value}
{assign var="tagwords" value=", "|explode:$taglist}
{foreach from=$tagwords item=tagword}
{assign var="tagword" value=$tagword|replace:",":""}
{cms_selflink page=$page_alias urlparam="&tag=`$tagword`" text=$tagword|trim}
{/foreach}
{else}
{$field->name}: {eval var=$field->value}
{/if}
</div>
{/foreach}
{/if}
</div>
{elseif empty($smarty.get.tag)}
<div class="summary">
{if $entry->postdate} <div class="date">{$entry->postdate|cms_date_format}</div>{/if}
<div class="title"><a href="{$entry->detail_url}">{$entry->title|escape}</a></div>
{if $entry->categories}
<div class="category">
{strip}{$category_label}
{foreach from=$entry->categories item=’category’}
{$category.name}
{/foreach}
{/strip}
</div>
{/if}
{if $entry->author}<div class="author">{$author_label} {$entry->author}</div>{/if}
{if $entry->summary}
<div class="summary">{eval var=$entry->summary}</div>
{else if $entry->content}
<div class="content">{eval var=$entry->content}</div>
{/if}
{if isset($entry->extra)}
<div class="CGBlogSummaryExtra">
{eval var=$entry->extra}
{* {cms_module module=’Uploads’ mode=’simpleurl’ upload_id=$entry->extravalue} *}
</div>
{/if}
{if isset($entry->fields)}
{foreach from=$entry->fields item=’field’}
<div class="CGBlogSummaryField">
{if $field->type == ‘file’}
<img src="{$entry->file_location}/{$field->value}"/>
{else}
{$field->name}: {eval var=$field->value}
{/if}
</div>
{/foreach}
{/if}
</div>
{/if}
{/foreach}
<!– End CGBlog Display Template –>
[/php]
Save it and make it the default “Summary Template”. Now take a look in the frontend and you will see, you can click the keywords.
FINISHED
Run the CGBlog module by inserting {CGBlog} into your desired page. Make sure your posts are published and contain keywords (tags).
Additional Notes: Just to understand the above code, you can read further. Here I just have added two important codesnippets.
[php]
{if !empty($smarty.get.tag) && ($entry->fieldsbyname.Tags->value|strpos:"`$smarty.get.tag`" !== false)}
[/php]
Here we look if the get paramater is set and not empty and if some of our entries contain the keyword.
IF TRUE, we show the template code with our keywords:
[php]
{if isset($entry->fields)}
{foreach from=$entry->fields item=’field’}
<div class="CGBlogSummaryField">
{if $field->type == ‘file’}
<img src="{$entry->file_location}/{$field->value}"/>
{elseif $field->name == ‘Tags’}
<h5>Tags</h5>
{assign var="taglist" value=$entry->fieldsbyname.Tags->value}
{assign var="tagwords" value=", "|explode:$taglist}
{foreach from=$tagwords item=tagword}
{assign var="tagword" value=$tagword|replace:",":""}
{cms_selflink page=$page_alias urlparam="&tag=`$tagword`" text=$tagword|trim}
{/foreach}
{else}
{$field->name}: {eval var=$field->value}
{/if}
</div>
{/foreach}
{/if}
[/php]
IF FALSE (elseif empty…) we show the regular code = all entries.
This example works well with CGBlog but you can actually apply it to any other module. I just wanted to show you a possible way to achieve this function.
Looking for quality CMS Made Simple Hosting? Look no further than Arvixe Web Hosting!
I’ve tried this with CMSMS 1.11.1 and CGBlog 1.9.8 and no luck 🙁
sorry. did not test it with cmsms1.11. maybe i will do this later.
Which versions of CMSMS and CGBlog did you test it with?
Cmsms 1.10.3.
I tried it on 1.10.3 and yes it works! Great job Nic Bug. What needs to be done to make it work on 1.11.1? We’ve been working hard on a web build on 1.11.1 and this would be the icing on the cake.
I have tried this on 1.11 the backend works great, but I can only see plain text field on front end? Any ideas what I need to change to make clickable – thanks
thats way to unspecific. i’m afraid i can’t help you. i need more details. which text field? what contains the text field? is there a template wrapped around the textfield?