How to bring in Latest topics when using category boxes

Hi Guys,

Our HR team have decided to go with category boxes instead of a category list as below.

However, they also would like the latest topics to appear at the bottom of the categories as seen in revoluts community page

I am struggling to work out how this can be done, either as an existing setting or with CSS,

any ideas?

2 « J'aime »

Try to work with components

You can use: categories-and-latest-topics

In this mode, there is a connection and categories and recent topics.

<script type='text/x-handlebars' data-template-name='components/categories-and-latest-topics'>

<div class='column categories'>
  {{categories-only categories=categories}}
</div>

<div class='column'>
  {{categories-topic-list topics=topics filter="latest" class="latest-topic-list"}}
</div>

</script>

Next, you override: categories-only

<script type='text/x-handlebars' data-template-name='components/categories-only'>

   The contents of the file...

</script>

Perhaps from this file it is necessary to move the box with the categories, or to do simply to change the css.

Options think very many.

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/templates/components/categories-boxes.hbs

Useful material

1 « J'aime »

Another option is to simply structure your category directly

https://meta.discourse.org/c/howto

4 « J'aime »

Then just change the css, which is even easier. )

2 « J'aime »

Ideally I would prefer to do it like you have suggested, however, there isnt a way to display categories in a grid layout and have the latest topics below. :frowning_face:

2 « J'aime »

I suggest building a static header customization if your categories do not change much. Then you could have whatever layout you like above the topic list.

2 « J'aime »

I’ve built this or something similar for a few sites at this point. If you put this in the </head> section of your theme it will put all the categories on top of your home page (and /latest assuming that it’s the same as your homepage)

Then all you’ll need is add the CSS to structure this layout however you want. This is a good introduction to using the API and Handlebars templates in general.

<script type="text/discourse-plugin" version="0.8">
  const ajax = require('discourse/lib/ajax').ajax;

  api.registerConnectorClass('below-site-header', 'custom-homepage', {
    setupComponent(args, component) {
      component.set('hostname', window.location.hostname);
      
      api.onPageChange((url, title) => {
        if (url == "/" || url == "/latest"){ 
          $('html').addClass('show-custom-homepage'); // Show homepage
          component.set('displayCustomHomepage', true);
          
            ajax("/site.json").then (function(result){ // Get list of categories
              let categoryName = [];
              result.categories.forEach(function(categories){
                categoryName.push(categories);
            });
            component.set('categoryName', categoryName);
          });  
        }
        else { // Hide homepage
          $('html').removeClass('show-custom-homepage');
          component.set('displayCustomHomepage', false);
        }
      });
    }
  });
</script>


<script type='text/x-handlebars' data-template-name='/connectors/below-site-header/custom-homepage'>
  
  {{#if displayCustomHomepage}}
  
  <div class="custom-homepage-wrapper"> 

    <div class="wrap wrap-category-boxes">
    
      <div class="homepage-category-boxes">
        {{#each categoryName as |c|}}
        <a href="/c/{{c.slug}}" class="box">
          <div class="homepage-category-box">
            {{#if c.uploaded_logo}}
            <div class="category-image-wrapper">  
              <img class="homepage-category-image" src="{{c.uploaded_logo.url}}" />               
            </div>              
            {{/if}}
            <h2>{{#if c.read_restricted}}<i class="fa fa-lock">&nbsp;</i>{{/if}}{{c.name}}</h2>
            <p>{{c.description}}</p>
          </div>
        </a>
        {{/each}}
      </div>
      
    </div>
    
  </div> 
  {{/if}}

</script>
10 « J'aime »

It looks very nice. How I can to add Font Awesome 5 instead of images? Many thanks!

1 « J'aime »

Hello,

Thank you for this code snippet! I am working on a project where I need to do some things on this principle (not necessarily categories) and I would like to put this directly in the plugin that I develop.

I know where to put the template handlebars, but I can not find the folder / file where to put the javascript. Could you tell me how to proceed?

Thank you,
Thymotep

1 « J'aime »

I love this idea! Looking forward to test such a theme component or plugin

1 « J'aime »

Is there an update to this? I’m getting an error from the script not being able to get the category names. Nothing is changing on the page.

Yes, there was an error! Thanks for reporting it. I’ve fixed the code in the post above.

We turned strict mode on for our JavaScript, which means categoryName = []; needs to be declared, changing that line to let categoryName = []; was the fix.

4 « J'aime »

Ce lien GitHub vers, je l’espère, le composant qui fait ce truc, est en 404.

1 « J'aime »

Je pense que ce composant se trouve maintenant ici :

https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/templates/components/categories-boxes.hbs

2 « J'aime »

D’accord… Je vais devoir trouver comment faire fonctionner un hbs comme quelque chose que je peux ajouter à Discourse. Je suppose que je dois l’utiliser comme un fichier dans un composant et que je dois faire un tutoriel à ce sujet ?
J’ai vu un tutoriel avec un thème simple. Est-ce la bonne direction à suivre ?

1 « J'aime »

Je n’ai jamais fait ça auparavant, donc je ne connais pas la réponse. Le tutoriel du développeur de thèmes serait certainement un bon point de départ.

Si vous recherchez « Mounting widgets » sur cette page, vous verrez un exemple d’affichage d’un widget dans une autre partie de la page.

Ce sujet howto pourrait également être utile comme exemple :

https://meta.discourse.org/t/how-to-add-a-featured-topic-list-to-your-discourse-homepage/132949

3 « J'aime »

Je vous tiendrai au courant de mon succès. En attendant, j’ai activé le thème Air et j’en suis très satisfait, mais j’aimerais toujours avoir les derniers articles sous les sujets de catégorie.

J’ai été programmeur msvc++ il y a 25 ans, et j’ai réussi à créer une application Flutter publiée sur le Play Store. Les API open source ne sont cependant pas faciles pour moi. Nous verrons comment les choses évoluent. Je posterai une mise à jour quand je le pourrai.

2 « J'aime »

Avez-vous réussi ? Je cherche à faire la même chose, je me demande s’il existe une solution simple ou si je dois plonger dans le thème et y passer du temps.

1 « J'aime »

En utilisant le style de page de catégorie de bureau “Boîtes avec sous-catégories”, j’ai pu créer un composant de thème et ajouter du code sous les boîtes. J’essaie maintenant de savoir comment lister les derniers sujets sur l’ensemble du site.

<script type="text/x-handlebars" data-template-name="/connectors/category-boxes-after-boxes/mwp">
<div class='column'>
  {{categories-topic-list topics=topics filter="latest" class="latest-topic-list"}}
</div>

</script>

Le problème est que cela indique simplement “Vous avez tout vu”.

1 « J'aime »

J’ai abandonné. J’utilise maintenant principalement des choses standard.

1 « J'aime »