How to add a topic in top nav in a specific category?


(François Douville) #1

I want to add a topic to a top menu but not on all categories, just a specific one.
What code should I write?
Thanks


Custom top navigation links
(François Douville) #2

Found those topics but none of them was for a specific category…


(Kris) #6

Tested this solution a bit more (modified from this post) try this (replace “Travel” with your category name):


<script>
Discourse.ExternalNavItem = Discourse.NavItem.extend({
    href : function() {
      return this.get('href');
    }.property('href')
  });

  I18n.translations.en.js.filters.bugs = { title: "Bugs", help: "Open Bugs" };
  I18n.translations.en.js.filters.google = { title: "Google", help: "Navigate to Google" };

  Discourse.NavItem.reopenClass({
    buildList : function(category, args) {
      var list = this._super(category, args);
      if(category && category.name == "Travel") {
        list.push(Discourse.ExternalNavItem.create({href: '/category/bug', name: 'bugs'}));
        list.push(Discourse.ExternalNavItem.create({href: 'https://google.com', name: 'google'}));
      }
      return list;
    }
  });
</script>


(François Douville) #7

Maybe a bug with translation [fr.filters.Liste.title]

And is it possible to move it here?


(Daniela) #8
I18n.translations.en.js.filters.bugs = { title: "Bugs", help: "Open Bugs" };
I18n.translations.en.js.filters.google = { title: "Google", help: "Navigate to Google" };

Those are for english based forum…

Your forum is french so use I18n.translations.fr instead


(François Douville) #9

Still the same thing with translations.fr

<script>
Discourse.ExternalNavItem = Discourse.NavItem.extend({
    href : function() {
      return this.get('href');
    }.property('href')
  });

  I18n.translations.fr.js.filters.liste = { title: "Liste", help: "Liste des formations" };

  Discourse.NavItem.reopenClass({
    buildList : function(category, args) {
      var list = this._super(category, args);
      if(category && category.name == "Formations") {
        list.push(Discourse.ExternalNavItem.create({href: '/t/liste-2-0-des-formations-sur-lautosuffisance-et-la-consommation-ecoresponsable-en-2018/306', name: 'Liste'}));
      }
      return list;
    }
  });
</script>

(Kris) #10

Try name: 'liste'instead


(François Douville) #11

It’s working, perfect! Doesn’t matter if it’s here. Thanks so much!