Code help - count number of cms items

Hi

I have seen this post - Adding a dynamic item that counts the number of posts in a collection

Anyone with experience of adding this to webflow? If so can you send me the code and how to get it to work? Perhaps there is a script out there somewhere, although having search extensively not found anything so far.

Thanks very much you lovely people…

2 Likes

so the trick could be to drag in the collection list, set it to display none, but still count the number of posts in jQuery:

CloudApp

<script>

blogNumber = $('.blog-post').length;
$('.blog-count').text(blogNumber);

</script>

hope this helps :slight_smile:

9 Likes

Thank you…that is exactly what I needed

1 Like

Hi, I’m trying this same code on my site to give a count of all blog posts, but I need it to be able to add multiple collection lists together. I have over 200 blog posts so I have a collection list displaying items 1-100 and then 101-200 and 201-300. is there a way to tweak this code to do this? Thank you!

3 Likes

Any updates on this one. Facing the same issue.

What to do to have more then 100 the max per page? is there a solution?

1 Like

Is this really heavy to load? I.e. does Webflow have to pull through all the CMS information each time in order to find that number?

Nelson, please help.
How do I display the number of items if I have a nested structure. Multi-reference.
Each post goes to another CMS collection via Multi reference, then I load some other posts from there. And that’s the value I want to display. How do I do that?
I need to use array brute force, but I can’t do it.

Hi how to apply this? what exactly to do? .)

Hi!
I need help to count nested CMS collection. I tried tis code but it’s count all nested elements
https://preview.webflow.com/preview/darias-dandy-site-62c9d9?utm_medium=preview_link&utm_source=designer&utm_content=darias-dandy-site-62c9d9&preview=34b9c97c0cf4f9076a86b2ead5c23740&workflow=preview

hi @d_onofrei to be able count categories for EACH article you need first grab all articles and apply code to EACH Article. Something like this:

const posts = document.querySelectorAll(".blog_post");
posts.forEach( p => {
const cats = p.querySelectorAll(".cat")

// create text node add it to DOM and add textContent
const textNode = document.createElement("paragraph") // or <span> or ...

// append node to the end of article or whatever ..
p.appendChild(textNode) 

// add text node
textNode.textContent = `There are ${cats.length} categories`;
1 Like

Thank you @Stan This works perfect!

1 Like

same problem for me. I have a collection with more than 100 items but the count stops at 100.

I’ve used a few approaches. All require different setups and custom code work.

  1. Drop multiple collection lists on the page. Range them, 1-100, 101-200, use JS to identify and count ( I prefer custom attributes for the identification ).
  2. Auto-gen your sitemap.xml, write script to retrieve it and to count items via a partial path match.
  3. Use Finsweet load. Use its API and/or the “item count” element to determine count. Note this changes as it loads, and will take awhile to arrive at the final count in a large list.
  4. Use a reverse proxy. Use API mods ( adds, deletes ) to update a KV store. Emit the KV data into the page, or make it accessible via an internal API.
1 Like