Style topics differently by category

Plugin Development
  • Hi there,

    Is there any way to style topics differently based on the category they belong to? The current page name is appended to the <body> tag as a class, but I can't find a way for the CSS to know anything about a topic's parent category.

    I can solve the issue with a javscript hack by reading the category name from the breadcrumbs, but this seems ... terrible.

    Is there a less terrible way to expose category information to the topic template?

  • You can do it with a plugin, listen for the hook filter:middleware.render and add the class you want to the bodyClass string if the user is on a topic page. Here is some code to get you going.

    // this goes in your plugin
    myPlugin.onPageRender = function (hookData, callback) {
      // this checks if we are on the topic page
      if (hookData.templateData.template.topic) {
        // add the class `category-<cid>` to bodyClass
        hookData.templateData.bodyClass += ' category-' + hookData.templateData.category.cid;
      callback(null, hookData);

    Hope that helps.

  • fantastic! my codebase is much happier now

Suggested Topics

  • 0 Votes
    3 Posts

    Hi @baris

    I tried a less block and now it's all working well.

    Thanks a lot!

  • 0 Votes
    4 Posts

    @julian unfortunately I will be using the support plugin in the same category because I only want the people to see their own topics and have access to reply to our responses

  • 0 Votes
    1 Posts

    Hi all,

    I'm working on a minor "experience" per post based plugin. User posts, it saves experience to an object. The saving works great, it displays fine. What I'm having an issue with is when someone replies to a topic. Everything stores correctly... but since the page doesn't refresh, the post doesnt populate with the updated information UNLESS they refresh the page.

    I've tried multiple hooks/filters and can't get any to work. The most appropriate seems to be "action:topic.reply". If I try to make a DB call to grab the object data, the action returns long before the DB call returns so none of the data I need gets added to the returned post object.

    Any ideas?

  • 0 Votes
    3 Posts

    Awesome ty so much 🙂
    I ended up making the call to Categories.getCategoryTopics() but I may switch back to the method you posted above 👍

  • 0 Votes
    1 Posts

    I'm looking to include custom forms on my site so that users can post uniform data for certain categories.

    What would be the best way to tackle this? Would I need to create an entire plugin for a new composer, just make a custom .tpl file, etc.?

    Looking for any suggestions or help! Attached are a few mockups of the form from an older website-

    0_1488395869241_Screen Shot 2017-03-01 at 1.11.29 PM.png
    1_1488395869241_Screen Shot 2017-03-01 at 1.12.33 PM.png