0

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; 
?>

1 Answer 1

1

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;
5
  • Thanks @kero, i get it. I used the get_post_type solution you proposed and it works perfectly. ;-)
    – studiok7
    Commented Nov 20, 2018 at 14:48
  • You're welcome! Using get_post_type() is cleaner here in my opinion. If in the future other post types will be in the list as well, you can then use switch e.g.
    – kero
    Commented Nov 20, 2018 at 14:49
  • What do you mean by "switch" ? And also a noob question, why that function needs 3 equal signs (means identical if i'm correct), what is the difference between "=", "==" and "===" ?
    – studiok7
    Commented Nov 20, 2018 at 14:53
  • switch() is a control structure. About the equal signs: you are correct. More info here or here. I prefer using === when possible, here it is no problem as both are strings
    – kero
    Commented Nov 20, 2018 at 15:25
  • 1
    I'll inspect that. Thank you again for all those clarifications ;-)
    – studiok7
    Commented Nov 20, 2018 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.