Page 1 of 1

CGBlog How To Show Category Name on Summary Page

Posted: Sat Oct 31, 2020 9:32 pm
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

Re: CGBlog How To Show Category Name on Summary Page

Posted: Sun Nov 01, 2020 10:07 am
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}

Re: CGBlog How To Show Category Name on Summary Page

Posted: Sun Nov 01, 2020 12:59 pm
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).

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

Posted: Sun Nov 01, 2020 2:48 pm
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.

Re: CGBlog How To Show Category Name on Summary Page

Posted: Sun Nov 01, 2020 2:59 pm
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>

Re: CGBlog How To Show Category Name on Summary Page

Posted: Sun Nov 01, 2020 3:09 pm
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!