Related CGBlog Articles

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm
Location: Florida

Related CGBlog Articles

Post by carasmo »

In the footer of my blog posts, on the detail page I have listed the related articles based on help from this forum to not print out the current article in other places on my site I also have smarty stuff based on help from this forum, particularly Robert Campbell, so combining some stuff, this here trick does the following:

Puts a related list of articles at the bottom of your blog posts based on categories that the post is assigned to, multiple categories, AND if there are none, the header "Related Articles" and the accompanying html holding those links is not printed out.

First make a SUMMARY CBBlog template called "related" in this example:

Code: Select all

{assign var='currentitem' value=$entry->id} 
{assign var='related1' value=0}
{foreach from=$items item=entry}
{if $entry->id !=$currentitem and $related1 == 0}
<section class="related clearfix"><h5>Related Articles</h5>
<ul>{assign var='related1' value=1}{/if}
{/foreach}

{foreach from=$items item=entry}
{if $entry->id !=$currentitem}
      <li>
         <a href="{$entry->detail_url}">
            {$entry->title|escape}
         </a>
      </li>
{/if}
{/foreach}

{assign var='related2' value=0}
{foreach from=$items item=entry}
{if $entry->id !=$currentitem and $related2 == 0}
</ul></section>
{assign var='related2' value=1}{/if}
{/foreach}
Next, in your DETAIL CGBlog template you will need to do put the following:

Code: Select all

{strip}{capture assign="categories"}{foreach from=$entry->categories item='one_category'}{foreach from=$categories item='one'}{if $one_category.name == $one.name}{$one.name},{/if}{/foreach}{/foreach}{/capture}{/strip}
This captures the categories for that blog Post and must come before the call to the related categories summary template

Then in the bottom of your blog DETAIL page as well, where you want the related categories to show up (if there are any), then paste the following. You will get a result like the screen shot (after applying your own css) IF there are related articles, but nothing if there are none.

Code: Select all

{CGBlog summarytemplate="related" category="$categories" number="6"}
Attachments
Screen Shot 2012-07-12 at 2.58.06 PM.png
Post Reply

Return to “Tips and Tricks”