In this tutorial, you will learn how to convert a paragraph block into a number counter without plugins.
128
256
810
1024
Add number counter in WordPress without plugins!
Add The Following Code to the functions.php File
In the WordPress dashboard, go to Appearance ➡ Theme File Editor and copy the following code into the child theme’s functions.php file and update it.
You must create a child theme before making any changes to functions.php
file. Otherwise, the applied changes will be lost after each update.
Create child theme in WordPress step by step [without plugin]
As an alternative method, you can use the Code Snippets plugin to insert your codes into WordPress.
/**
* Number Counter in WordPress by WPCookie
* https://redpishi.com/wordpress-tutorials/number-counter/
*/
add_filter( 'wp_footer', function ( ) { ?>
<script>
const counters = document.querySelectorAll('.counter');
if (counters) {
const observer = new IntersectionObserver(entries => {
entries.forEach( entry => {
if ( entry.isIntersecting ) {
const finalNumber = entry.target.innerText;
const time = Math.ceil(finalNumber / 30) ;
entry.target.innerText = 0;
const animate = () => {
const data = +entry.target.innerText;
if(data < finalNumber) {
entry.target.innerText = Math.ceil(data + time);
setTimeout(animate, 50);
}else{
entry.target.innerText = finalNumber;
}
}
animate();
observer.unobserve(entry.target);
}
})
}, {threshold: 0.1 })
counters.forEach( counter => {
observer.observe(counter)
});
}
</script>
<?php });
Convert a paragraph block to a number counter
Add a paragraph block, write a number inside it, and assign it the “counter” class. That’s it, you can also change the style of the paragraph to your liking.
For example, the above pattern is made by the following blocks. A row block in which there are four groups and in each group there is an image block and a paragraph with the “counter” class.
If this article is difficult for you to read in text, you can watch the video version below.