Forum Discussion

iftomkins's avatar
11 years ago

Get ID of current category

In implementing our mobile skin, there is one page where we want to list all the boards from multiple categories. In order to avoid having Lithium re-build the entire mobile browsing component for us, here is the workaround I'm exploring:

 

1. Get the ID of the current category

2. If the current category ID matches "product", then run a script with Freemarker + Javascript

3. The freemarker script loops through the 2 different categories we want to output on the product page (instead of just links to the two categories themselves), and outputs a complete board list.

4. The content on the page (previously listing the boards from product) is now replaced via Javascript with the board list from the 2 categories (which are both subcategories of the "product" category) via Javascript.

 

I'm stuck already at getting the ID of the current category. Here's a few different things I've tried based on posts I've found in the lithosphere, but it just keeps outputting everyting except the current node. I really just wnat ${currentNode.id} but I don't think that exists.

 

<#assign ancestors = coreNode.ancestors />
<#assign ancestorsReverse = coreNode.ancestors?reverse />
${ancestorsReverse[0].id} <!-- outputs core community ID -->
${ancestors[0].id} <!-- outputs core language ID -->
${coreNode.id} <!-- outputs parent node ID -->

 

${coreNode.id} is the closest, as it outputs the parent node. But how do we get the current node?

 

Cheers,

Alan

  • What page are you on when you're trying to output this? ${coreNode.id} shouldn't be outputting the id of the parent node. It should be the id of the current node. If you're on a community-level page (i.e. the front page, my settings, help pages, etc.) this will be the id of the community. If you're on a category page, this will be the category id. If you're on a a board page or topic page, it should give you the id of the board.

     

    The sample code Claudius provided should give you the id of the parent category if you're on a board page or topic page. If you're on a category page, you should just be able to use ${coreNode.id}. So depending on what pages your component appears, you may need to check what page you're on and adjust how you pull the id.

  • AdamN's avatar
    AdamN
    Khoros Oracle

    What page are you on when you're trying to output this? ${coreNode.id} shouldn't be outputting the id of the parent node. It should be the id of the current node. If you're on a community-level page (i.e. the front page, my settings, help pages, etc.) this will be the id of the community. If you're on a category page, this will be the category id. If you're on a a board page or topic page, it should give you the id of the board.

     

    The sample code Claudius provided should give you the id of the parent category if you're on a board page or topic page. If you're on a category page, you should just be able to use ${coreNode.id}. So depending on what pages your component appears, you may need to check what page you're on and adjust how you pull the id.

  • Hi Alan,

    I guess the easiest way to get hold of the current node's direct category is using REST API as follows:

    <#assign category = restadmin("/boards/id/${coreNode.id}/category/id").value />