Skip to content
Advertisement

WordPress Loop Post Counter Never Changes

I want to style the first post differently so I am trying to use a simple counter that will add a class to the first post.

First, on index.php I have this

JavaScript

and then on content.php I have

JavaScript

but $postcount is always 1

If I move $postCount = 0; and $postCount++; to content.php the value never changes either.

I can create a custom Blog page template but I would like to see this working.

Advertisement

Answer

You would have to apply a filter post_class. The below code came from this article: https://www.isitwp.com/add-class-to-first-post-in-the-loop/

JavaScript

Let me know if you have any questions! 🙂

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement