CGBlog How To Show Category Name on Summary Page

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
montedavis
Forum Members
Forum Members
Posts: 64
Joined: Wed Jul 03, 2013 7:44 pm

CGBlog How To Show Category Name on Summary Page

Post by montedavis »

Hello,

I'm trying to display category names on my summary pages of CGBlog.

I'm using BrowseCat to show a page with three categories, this works fine.

Next when clicking on a category link I go to the summary page and can see my category specific articles and I want to show the category name for the articles above the articles.

I tried adding the following to the summary page template but it did not work:

{if $entry->categories}
{foreach from=$entry->categories item='category' name=cat}
{if $category.category_id == $param_category_id}
{assign var='categoryname' value=$category.name}
<p>{$categoryname}</p>
{/if}
{/foreach}
{/if}

I thought this code would use the category_id for the category the articles were from on the page.

Thanks for any help!

Monte
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: CGBlog How To Show Category Name on Summary Page

Post by Rolf »

At his blog:
https://cmscanbesimple.org/blog/breadcr ... ews-module
I use:

Code: Select all

{if $entry->categories}{$entry->categories[0]['name']}{/if}
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3484
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CGBlog How To Show Category Name on Summary Page

Post by velden »

In your case it could be a little different as you're on a summary page.

IF the page has articles AND IF all items have one (and only one) category, you could use this code to create the heading before the foreach loop:

Code: Select all

{if $items[0]->categories}{$items[0]->categories[0]['name']}{/if}
If one of the above conditions is not always true there are two ways to cat the category name:
- query the database using a UDT and get the name based on the id in ($actionparams['category_id'])
- save the category_id and call CGBlog's browsecat action with a template that only displays the name of the category that matches the saved id (inefficient).
montedavis
Forum Members
Forum Members
Posts: 64
Joined: Wed Jul 03, 2013 7:44 pm

[SOLVED] Re: CGBlog How To Show Category Name on Summary Page

Post by montedavis »

Thank you Rolf and Velden! I really appreciate you both posting. Your posts both have helped me. As Velden mentioned I am on a summary page, really I'm using the browsecat action to display specified categories, which is working but I was looking to add the category name the user chooses on the articles summary page using the $param_category_id. I would have to match check the categories array for a category.id that matched the $param_category_id. I like Velden's approach as I will have multiple categories per article and would prefer not to use the first category as the presented page category name. Thanks again for posting I appreciate your help.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3484
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CGBlog How To Show Category Name on Summary Page

Post by velden »

Quick and dirty (needs some checks and error handling actually) but you get the idea:

UDT name: print_cgblog_cat_name

Code: Select all

$db = cmsms()->GetDb();
$query = 'SELECT long_name FROM ' . cms_db_prefix() . 'module_cgblog_categories WHERE id = ?';
$category_name = $db->GetOne($query,array($params['catid']));
echo $category_name;
Example usage in summary template for category:

Code: Select all

<h3>{print_cgblog_cat_name catid=$actionparams['category_id']}</h3>
montedavis
Forum Members
Forum Members
Posts: 64
Joined: Wed Jul 03, 2013 7:44 pm

Re: CGBlog How To Show Category Name on Summary Page

Post by montedavis »

Velden, thank you again! I will give this a try, I had no idea on how I was going to create the UDT LOL!
Post Reply

Return to “CMSMS Core”