Pagination won’t work on custom page

Ok, I found the solution for this specific case, since it’s a template for the articles.

On the admin settings, define the custom page as the page for the articles. And using the code I posted in the question, everything works just fine.

Now, if you are using a page for the homepage, another for the articles, and you have another custom page that needs pagination, this solution won’t work.

If anyone knows how to fix that, it would be good to leave the info here.