Home › Forums › Calendar Products › Events Calendar PRO › Custom Loop that Displays Month Headers and Conditionally Displays Categories
- This topic has 4 replies, 3 voices, and was last updated 10 years, 9 months ago by
Support Droid.
-
AuthorPosts
-
July 2, 2015 at 12:04 pm #974807
stephenschildbach
ParticipantI hope it is ok for me to combine two questions in one because they both refer to coding a custom event loop. Thank you for you help ahead of time.
1) How would I write code that puts Month headers in the appropriate location so that a list of events can be separated by their start date month?
June
Event #1
Event #2
July
Event #3
Event #4
etc…
2) How would I write a conditional statement that only displays the category if the category slug is “featured_event” or “mahasangha”
My loop below works without these two parameters, but every time I try applying the above two additions, my code breaks or does not work.<div class="left-col-event-list"> <h2>Upcoming Events</h2> <?php global $post; $list_events = tribe_get_events(array( 'eventDisplay'=>'upcoming', 'posts_per_page'=>10, 'post_type'=>'tribe_events', 'taxonomy'=>'tribe_events_category' )); if (count($list_events) == 0) { echo '<p>No Events at this time.</p>'; } else { foreach($list_events as $post) { setup_postdata($post); ?> <p><strong><a href="<?php the_permalink(); ?>" class="title-link"><?php the_title(); ?></a></strong><br> <strong>Member Group:</strong> <?php the_author(); ?><br> <?php echo '<strong>Starts:</strong> ' . tribe_get_start_date($post, false, $format = 'D, M jS' ) . '<br>'; echo '<strong>Region:</strong> ' . tribe_get_city( $event_id ); echo ', '; echo tribe_get_region( $event_id ); echo '</p>'; }} //endforeach ?> <?php wp_reset_query(); ?> </div>July 3, 2015 at 6:46 am #975158Barry
MemberGreat questions! I do need to note however that the level of support we can offer for custom coding issues is fairly limited, but I’d be happy to point you in the right direction if I can 🙂
1) How would I write code that puts Month headers in the appropriate location so that a list of events can be separated by their start date month?
This is in fact what our own list view does, so looking at our code could give you an idea about this. At a high level, the logic is something like this:
- At the end of each loop, keep a record of the last event’s date
- When starting the loop, if we don’t have such a record it must be the first event: so display the header
- Otherwise, check to see if the current event is in a different month to the last event’s date: only display the header if that condition is true
2) How would I write a conditional statement that only displays the category if the category slug is “featured_event” or “mahasangha”
First you need to get a list of all the applied categories – you could possibly use tribe_get_event_taxonomy() for this (it returns a single string, so you might need to further break it down), though working directly with WordPress’s own get_the_terms() function may be cleaner (as it returns an array of terms) – if you take that route, please note the taxonomy slug for event categories is:
tribe_events_cat
Once you’ve got the list, you can loop through it and only display the terms you want.
I hope that helps 🙂
July 5, 2015 at 11:11 pm #975989stephenschildbach
ParticipantBarry, I can understand why you can’t do more, because then it would be like giving WordPress and PHP lessons. You say, “This is in fact what our own list view does…”. What file in the plugin would I find the loop creating a month header. Not the top level file that just has one line of code, but the actual loop doing, “At the end of each loop, keep a record of the last event’s date”, as you say. Overall, I can see I just need to get better at my WordPress loops and PHP. Thank you.
July 6, 2015 at 5:52 am #976035Barry
MemberHi there 🙂
The loop itself lives is created in src/views/list/loop.php – but the logic for the month headers lives in the tribe_events_list_the_date_headers() function which is located in src/functions/template-tags/loop.php.
The implementation is slightly different to what I outlined in that it checks its position within the loop and access the previous post if one exists (rather than storing the information itself), but the basic idea can still be seen here.
I hope that helps – and good luck!
July 21, 2015 at 7:05 am #988344Support Droid
KeymasterThis topic has not been active for quite some time and will now be closed.
If you still need assistance please simply open a new topic (linking to this one if necessary)
and one of the team will be only too happy to help. -
AuthorPosts
- The topic ‘Custom Loop that Displays Month Headers and Conditionally Displays Categories’ is closed to new replies.
