基于Tribe Events插件按活动类别展示不同信息的实现求助
Hey there! Let's get this sorted for you—displaying custom content after a Tribe Events event ends based on its category is totally doable, and I’ll walk you through a reliable approach that fixes common pitfalls people run into.
First, make sure you’re correctly verifying two key things:
- Is the event actually past/ended?
- What category(ies) is the event assigned to?
Tribe Events has built-in functions to handle event status, and we’ll use WordPress core functions to pull the event’s categories (note: Tribe uses the tribe_events_cat taxonomy specifically for event categories).
Add this code to your theme’s functions.php file (or a custom plugin—this is safer to avoid losing changes during theme updates):
add_action( 'tribe_events_single_event_after_the_content', 'display_custom_content_after_ended_event_by_category' ); function display_custom_content_after_ended_event_by_category() { // Exit if we're not on a single event page if ( ! is_singular( 'tribe_events' ) ) { return; } // Grab the current event's ID $event_id = get_the_ID(); // Check if the event has ended using Tribe's official function if ( ! Tribe__Events__Main::post_is_past( $event_id ) ) { return; // Skip if event is ongoing or future } // Get the event's assigned categories (Tribe's custom taxonomy) $event_categories = get_the_terms( $event_id, 'tribe_events_cat' ); // Exit if no categories are found or there's an error if ( empty( $event_categories ) || is_wp_error( $event_categories ) ) { return; } // Loop through categories to match slugs (adjust these to your actual category slugs) foreach ( $event_categories as $category ) { switch ( $category->slug ) { case 'workshop': echo '<div class="ended-event-content workshop-style"> <h3>Thanks for joining our workshop!</h3> <p>Download the session slides and submit your feedback using the links below.</p> </div>'; break; case 'conference': echo '<div class="ended-event-content conference-style"> <h3>Great connecting with you at the conference!</h3> <p>Check out event photos and exclusive resources in your attendee dashboard.</p> </div>'; break; case 'webinar': echo '<div class="ended-event-content webinar-style"> <h3>Missed the live webinar?</h3> <p>Watch the full replay and grab the companion guide right here.</p> </div>'; break; // Add more cases for your other event categories default: // Optional: Default content for uncategorized events echo '<div class="ended-event-content default-style"> <h3>Thanks for participating!</h3> <p>Stay tuned for more updates and upcoming events from us.</p> </div>'; break; } } }
If your original code didn’t work, it’s probably one of these issues:
- Wrong taxonomy: You might have used WordPress’s default
categoryinstead of Tribe’stribe_events_cat. Test withvar_dump( $event_categories )to see what data you’re actually pulling. - Manual date checks: Avoid comparing dates manually—
Tribe__Events__Main::post_is_past()accounts for multi-day events and all Tribe’s event settings. - Incorrect hook: Make sure you’re using
tribe_events_single_event_after_the_contentto ensure your content appears in the right spot on single event pages. - Slug mismatches: Category slugs are case-sensitive! If your category is named "Workshop" but the slug is "workshop", use the lowercase version in your
switchstatement.
Add this CSS to your theme’s stylesheet to make the content look polished:
.ended-event-content { margin: 2rem 0; padding: 1.5rem; border-radius: 8px; background-color: #f8f9fa; } .ended-event-content h3 { margin-top: 0; color: #2d3748; } .workshop-style { border-left: 4px solid #3182ce; } .conference-style { border-left: 4px solid #2f855a; } .webinar-style { border-left: 4px solid #ecc94b; }
This approach uses Tribe’s official APIs, so it’s update-safe and avoids breaking when the plugin gets updated.
内容的提问来源于stack exchange,提问作者Costin Andrei

