I have a CPT for events with a dateline field. In a Loop I want to show all upcoming events.
For design purposes I want to „cluster“ all events from month to month. For this I need to determine in the loop of a view that the current event in the loop belongs to the some / a new month like the event before (events are sorted by the date of the event…).
Is there a was to achieve this? Like having a variable to store info in and compare it? Or a function to do so?
There are a couple of ways you might do this, depending on whether you are more comfortable with PHP or CSS.
The CSS solution would be to add a dynamic class to the container div with the value of the month from the current post (use a shortcode to add the class name).
Then add CSS rules for the even months to give them one colour background, and rules for the odd months to give them a different colour background. Then you'll have a zebra effect. (You'll want to remove margins between the containers to have solid blocks of background colour for each month.)
The PHP alternative would be to declare a custom shortcode that you add to the loop output section. That shortcode would declare a static variable used to store the value of the month of the current loop iteration. A static variable retains its value each time it is called, so when the shortcode runs again (a new loop iteration) it will have the value from the previous iteration, so you can do a simple check to see if the value has changed (from one month to another month), and if it has, return something like an hr tag to provide a visual break between months.