Exclude one news category

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
JohnnyB
Dev Team Member
Dev Team Member
Posts: 729
Joined: Tue Nov 21, 2006 5:05 pm
Location: OH, USA

Exclude one news category

Post 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
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
User avatar
relic
Forum Members
Forum Members
Posts: 34
Joined: Fri Dec 08, 2006 9:59 am
Location: New Zealand

Re: Exclude one news category

Post by relic »

hello,

why don't you just call the category's you only want to display??
beherenow_uk
Forum Members
Forum Members
Posts: 103
Joined: Fri Nov 28, 2008 11:26 am

Re: Exclude one news category

Post 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.
Last edited by beherenow_uk on Tue Dec 07, 2010 10:59 am, edited 1 time in total.
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Exclude one news category

Post by jmcgin51 »

x3

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

Re: Exclude one news category

Post 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'}
Last edited by uniqu3 on Tue Dec 07, 2010 4:33 pm, edited 1 time in total.
andershz
Forum Members
Forum Members
Posts: 49
Joined: Fri Nov 21, 2008 9:30 pm
Location: Sweden

Re: Exclude one news category

Post 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.
Last edited by andershz on Wed Dec 08, 2010 7:49 am, edited 1 time in total.
Post Reply

Return to “CMSMS Core”