ZD Guide: Display category ID | Community
Skip to main content

ZD Guide: Display category ID

  • September 8, 2022
  • 3 replies
  • 0 views

Andrea22

On the page displaying an article, we want to access the ID of the article, the ID of the section containing it and the ID of the category where they are.

If I go editing the templates/article_code.hbs I can use {{article.id}} and {{section.id}}.

I cannot find a way to access the category. {{section.id}}, {{section.category.id}}, {{section.category_id}} are all not working: I cannot save the template because of a message like "not possible to access `category` in `section.category.name`".

Is the category accessible?

3 replies

Andrea22
  • Author
  • September 9, 2022

Thanks Ifra,

The tip about finding information with Javascript in the breadcrumbs was useful! The tip uses jQuery to extract the category ID, but we prefer a pure Javascript approach.

I came up with some code to get the category name. Clicking the button will open the URL in the link, with the category name as argument.

WARNING!!! The code below does the job for me now, but should be improved before using it!

<script>
  var categoryName = document
    .getElementsByClassName('breadcrumbs')[0]
    .getElementsByTagName('li')[1]
    .title;
</script>
<div id="look-it-up">
  <a class="button" href="https://duckduckgo.com/?q=" onclick="location.href=this.href+categoryName;return false;">Look it up!</a>
</div>

What does it do?

The button in the <A> tag links to a URL. When clicked, it runs the script in the page to extract the categoryName and appends it to the link


Ifra
  • September 9, 2022

Hey Andrea, can you share your query that exactly what do you want to do?


Andrea22
  • Author
  • September 12, 2022

Hi @ifra,

Sure (and thanks again for the help)!

My company uses a third party service to analyze the feedback received from customers. We want to start using it also when customers leave feedback about a support article.

The button to leave feedback opens a short survey. We want to pass as URL parameters also the ID of the article, together with the section and category it is in. The survey stays the same, but our partner knows what the feedback is about, improving the analysis.

PS: I did not name our business partner here: I am not sure it is allowed and it would not really briing much more information. The only difference between our production code and the one above is the content of the href