Page 1 of 1

Exclude one news category

Posted: Thu Dec 20, 2007 3:49 am
by JohnnyB
I understand how to show only certain categories in the News module... category="category1, category2"

Is it possible to exclude a category?  I am using a category for a specific function and do not wish it to show up on the 'news' page.  And, to make things easier for editors that may add categories, I'd like to just exclude the one that shouldn't be included on the news page instead of relying on the editors to add their new category to category paramater in the News smarty tag.

Thanks! ;D

Re: Exclude one news category

Posted: Thu May 01, 2008 6:28 am
by relic
hello,

why don't you just call the category's you only want to display??

Re: Exclude one news category

Posted: Tue Dec 07, 2010 10:57 am
by beherenow_uk
Hi,

I also have this need. We're adding news categories all the time, and so its not really viable to be updating the pages where theyre ALL called just to exclude a single category. Is there something whereby I can just write

Code: Select all

{news exclude='mysecretcategory'}
or something like that? (this would show all categories except 'mysecretcategory')

Thanks.

Re: Exclude one news category

Posted: Tue Dec 07, 2010 2:59 pm
by jmcgin51
x3

Time to add a feature request for the News module in the Forge.

Re: Exclude one news category

Posted: Tue Dec 07, 2010 4:29 pm
by uniqu3
Peciura helped me on same problem when i was working on my Blog so i think the code could be changed for News to.

Create a UDT, mine is named blogcat:

Code: Select all

/**
* Get CGBlog category comma separated list.
*
* @params       string $params['exclude']       comma separated list of categories to be excluded.
* @params       string $params['noslashes']        Add slashes from category names ( added for better DB compatibility).
* @params       string $params['assign']        Assign value to.
*
*/
 
$cat_delim = ',';
$return = '';
$cat_array = array();
$cat_exclude = array();
$db = cmsms()->GetDB();
$query = "SELECT `name` FROM ".cms_db_prefix()."module_cgblog_categories ORDER BY sort_order";
 
$dbresult = $db->Execute($query);
 
while ($dbresult && $row = $dbresult->FetchRow())
{
        if(empty($params['noslashes'])){
                $row['name'] = addslashes($row['name']);
        }
        $cat_array[$row['name']] = $row['name'];
}
 
if (!empty($params['exclude'])){
        $cat_exclude = explode($cat_delim, trim($params['exclude']));
        foreach($cat_exclude as $name){
                $name = trim($name);
                if (isset($cat_array[$name])){
                        unset($cat_array[$name]);
                }
        }
}
 
$return = implode($cat_delim, $cat_array);
 
if(!empty($params['assign'])){
        $smarty = cmsms()->GetSmarty();
        $smarty->assign(trim($params['assign']), $return);
}
else{
        return $return;
}
Now use this call:

{blogcat exclude='Your Category' assign='cat_list'}{CGBlog category=$cat_list summarytemplate='your Template'}

Re: Exclude one news category

Posted: Tue Dec 07, 2010 7:36 pm
by andershz
Another way of doing it:

1.
Go to Content->News->Browse Category Templates
Create a new template, (I called it Test),  with the following content:

Code: Select all

{if $count > 0}
{foreach from=$cats item=node}
{$node.news_category_name},
{/foreach}
{/if}
2.
Go to Extensions->User Defined Tags
Create a new UDT, (called Remove), with the following contents:

Code: Select all

$remove = explode( ',', trim( $params['remove']));
$input = explode( ',', trim( trim( $params['input']), ','));
$input = array_map( trim, $input);
echo implode( ',', array_diff( $input, $remove));
3.
In the page where you want the news insert something like this:

Code: Select all

{capture assign="all_cats"}{news action="browsecat" browsecattemplate="Test"}{/capture}
{capture assign="cats"}{Remove input=$all_cats remove="General,Major"}{/capture}
{news category=$cats}
This will show all news except from the categories General and Major.

If you always want to exclude the same category you could simplify things by removing it with smarty logic already in the browsecat template and skip the UDT.
But the UDT method is more flexible.

Edit:
Note that this method does not correctly handle sub categories.
If you need those it must be slightly modified.