tribe_get_start_time displays the current date and time on other post types than tribe_events

i am displaying posts selected by an ACF relationship (with object format) on a page, including custom posts like tribe_events. I want to display the date and time on the tribe_events posts, but not on the other posts. I am using this code below. It displays correctly the start date and time of the tribe_events, but it also displays the current date and time on the other posts. What can i don to have the date and time only on the tribe_events posts ? Thanks ;-)

?php
global $post;
$posts = get_field( 'relationship' );
if( $posts ): 

foreach( $posts as $p ):
setup_postdata( $p );

// Display the date of the event
$p_event = tribe_get_start_time ( $p-ID, 'j F à H \h i' );

if ( $p_event ) { 
    echo $p_event;          
}
else '';
endforeach;
endif; 
?

Topic the-events-calendar date-time posts Wordpress

Category Web


There are various ways to solve this. First coming to mind are checking via is_singular() or get_post_type(). Using the latter you could write

foreach( $posts as $p ):
    setup_postdata( $p );

    $post_type = get_post_type($p);

    if ($post_type === Tribe__Events__Main::POSTTYPE) {
    //or: if ($post_type === 'tribe_events') {

        // Display the date of the event
        $p_event = tribe_get_start_time ( $p->ID, 'j F à H \h i' );

        if ( $p_event ) { 
            echo $p_event;          
        }
        else '';
    }
endforeach;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.