Can Webflow display the number of posts in a collection next to a link?

Published on
September 22, 2023

Yes, Webflow can display the number of posts in a collection next to a link. This can be achieved by using Webflow's dynamic list feature along with some custom code.

Here's a step-by-step guide on how to accomplish this:

  1. Create a collection for your posts: Go to the Webflow Designer and navigate to the Collections section. Create a new collection and define the necessary fields for your posts.

  2. Build your dynamic list: On a page where you want to display your collection's posts, add a dynamic list element. Connect it to your collection and design the layout of your list items using the elements within the dynamic list.

  3. Add a link to your list item: Inside the dynamic list item, add a link element that will link to the post. You can create a text link or use a button, depending on your design preference.

  4. Display the post count: To display the number of posts next to each link, you'll need to use custom code. Wrap the link element with a div container and give it a custom class.

  5. Add custom code: Go to the page settings, select the Custom Code tab, and add the following JavaScript code inside the <head> tag:

  <script>  window.addEventListener('DOMContentLoaded', function() {    var collectionList = document.querySelectorAll('.your-custom-class');    var postCount;    for (var i = 0; i < collectionList.length; i++) {      var collectionSlug = collectionList[i].getAttribute('data-collection-slug');      fetch(`/api/collections/${collectionSlug}`)        .then(response => response.json())        .then(data => {          postCount = data.items.length;          var postCountElement = document.createElement('span');          postCountElement.innerText = postCount;          collectionList[i].appendChild(postCountElement);        });    }  });  </script>

Make sure to replace .your-custom-class with the class name you used for the div container around your links.

  1. Publish your site: Once you've added the custom code, publish your site for the changes to take effect.

By following these steps, you will be able to display the number of posts in a collection next to each link on your Webflow site. This can be helpful to provide users with insights into the content available in each collection and encourage them to explore further.

Additional Questions:

  • How can I create a dynamic list in Webflow?
  • Can I customize the design of the post count displayed next to the link?
  • Is there a way to display the number of posts in a collection without using custom code?