Recent Topics

current cattegory

Started by on Feb 23, 2008 – Contents updated: Feb 23, 2008

Feb 23, 2008 09:49    

How can I get the category for the page I am on?

I need this for a breadcrumb and link plugin I'm writing for my integration with phpbb and mediawiki. Where the categories in b2e are the same as a certain forum in phpbb and a category in mediawiki.

I know a post can belong to seveal categories but that should not be a problem. For the breadcrumb I just pick the first one ;) and for the links I can have several.

TIA for any help.

Feb 23, 2008 11:46

Try the following

<?php
		echo ' T_('Categories'), ': ';
		$Item->categories();
?>

Feb 23, 2008 14:19

Thanks.

That didn't work. I already got the category id by doing this

global $cat;
print $cat;

but that is only one id.

I tried with $Item even adding a global $Item. but that didn't give the anything $Item seemed to be NULL.

Feb 23, 2008 15:22

Are you looking for a list of all main categories on any given page? Like for example if you have multiple posts on a page you want to know multiple main cat IDs?

In fact instead of trying to explain how about showing us a link to a couple of pages and tell us what you want to do with category info from across those pages?

Feb 23, 2008 16:02

EdB wrote:

Are you looking for a list of all main categories on any given page? Like for example if you have multiple posts on a page you want to know multiple main cat IDs?

In fact instead of trying to explain how about showing us a link to a couple of pages and tell us what you want to do with category info from across those pages?

I would like to give you some pages but I can't just yet, because it is still on our dev-server. Sorry about that.

But I think the $cat will do for now.

altough:

"Are you looking for a list of all main categories on any given page? Like for example if you have multiple posts on a page you want to know multiple main cat IDs?"

this would be nice too ;)

Feb 23, 2008 16:20

Ok, this may or may not work :

<?php
global $MainList;
$allMainCats = array();
while( $Item = & mainlist_get_item() )
{
$allMainCats[] = $Item->main_cat_ID;
}
$MainList->restart();

echo '<p>Main cats are : '.implode( ', ', $allMainCats ).'</p>'."\n";
?>

¥

Feb 23, 2008 16:37

Yep that worked, thanks very much to all.

Feb 26, 2008 16:49

I ran into this, as well. You might want to use the main category for an item, instead of just the $cat, in case you end up with nested categories or multiple categories for the same post.

Also note if you have multiple categories given (like the category list form which allows you to select multiple categories at once), the $cat will probably be empty.

For the main category, I used, like some of the others mentioned,

$cat_ID = $Item->main_cat_ID;

For the category, I used the following function, but note I assumed if multiple categories were selected, I arbitrarily decided I wanted the first one (I don't have the category multiple-selection available to the user anyway)



   function get_current_category()
   {
      $curr_cat = NULL;

      if ( isset( $GLOBALS[ 'catsel' ] )  )
      {
         $curr_cats = $GLOBALS[ 'catsel' ];
         if ( count ( $curr_cats ) > 0  && $curr_cats[ 0 ] > 0 )
         {
            $curr_cat = $curr_cats[ 0 ];
         }
      }
           
      if ( NULL == $curr_cat && isset( $GLOBALS[ 'cat' ] ) && $GLOBALS[ 'cat' ] > 0 )
      {
         $curr_cat = $GLOBALS[ 'cat' ];
      }

      return $curr_cat;
   }

You can see this code in action at www.thewildlifeporch.com with the vertical tabs on the left -- the current (root) category is displayed as selected. You can also see it with the breadcrumb category trail under the title of the post -- I end with the post's main category.

Hope this helps!

Cindy Rae


Form is loading...

Run your own website! – This forum is powered by b2evolution CMS, a complete engine for your website.