PHP – If Event Has Featured Image

Home Forums Calendar Products Events Calendar PRO PHP – If Event Has Featured Image

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1345790
    Daniel
    Participant

    Hi there. I have created a child template, and I have been modifying it fine.

    I have been stuck on this issue for two hours now, and my brain is fried, so I decided to ask you guys.

    I need to write a script to create a div if an event has a featured image. Otherwise I need the div not to exist on the events that don’t have a featured image.

    I have tried using jQuery to test $(this).hasClass(“tribe-events-event-image”) and I have tried using PHP to see if the response from tribe_event_featured_image is true or null, but I can’t figure it out.

    So how do I test if the event has a featured image or not?

    #1346304
    snorton
    Participant

    If you’ve followed the steps for overriding the template for single-events.php then all you have to do in this case is replace the line that calls the featured image in the PHP file with the following:

    if(has_post_thumbnail($event_id)) {
      echo '<div>' . tribe_event_featured_image( $event_id, ‘full’, false ) . '</div>'; 
    }

    The same principle applies to any instance on other template override pages where the post_thumbnail is called.

    You could even write a filter in your functions.php file that changes the html surrounding the image source:

    function custom_event_featured_image_html( $featured_image, $post_id, $size ) {
    	$link = true;
    	$image_html = get_the_post_thumbnail( $post_id, apply_filters( 'tribe_event_featured_image_size', $size, $post_id ) );
    	if ( ! empty( $image_html ) && apply_filters( 'tribe_event_featured_image_link', $link ) ) {
    		$featured_image .= '<div><a href="' . esc_url( tribe_get_event_link() ) . '">' . $image_html . '</a></div>';
    	}
    	elseif ( ! empty( $image_html ) ) {
    		$featured_image .= '<div>' . $image_html . '</div>';
    	}
    };
    add_filter( 'tribe_event_featured_image', 'custom_event_featured_image_html', 10, 3 );

    Modify as needed 🙂

    Cheers.

    #1346603
    Courtney
    Member

    Thanks @snorton! That looks like a great start for @djb.

    Daniel, can you give that solution a try and let us know how it goes? We are happy to proofread a bit of code, but we’re limited in helping with customizations, per our Scope of Support / Terms.

    Thanks
    Courtney 🙂

    #1349029
    Daniel
    Participant

    Ok, in my themeFolder/tribe-events/list/single-event.php I have

    
    <!-- Event Image -->
    <?php echo tribe_event_featured_image( null, 'medium') ?>
    <?php
    if(has_post_thumbnail($event_id)) {
      echo '<div>Hello</div>'; 
    }
    ?>
    

    But on the page it prints

    
    if(has_post_thumbnail($event_id)) { echo '
    Hello
    '; } 
    

    I didn’t replace the image code like you said, but added it in under so the image would show up.

    • This reply was modified 6 years, 7 months ago by Daniel.
    • This reply was modified 6 years, 7 months ago by Daniel.
    • This reply was modified 6 years, 7 months ago by Daniel.
    • This reply was modified 6 years, 7 months ago by Daniel.
    • This reply was modified 6 years, 7 months ago by Daniel.
    #1349036
    Daniel
    Participant

    Nevermind. I don’t know why it updated all of a sudden, but it did. Ignore that last post. The page now says hello where it originally had the php code.

    Thank you for the helpful code and tips!

    #1349101
    Courtney
    Member

    Thanks for the update Daniel. I’m glad it is working now. Do let us know if you need further help.

    Thanks
    Courtney 🙂

    #1360372
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘PHP – If Event Has Featured Image’ is closed to new replies.