Home › Forums › Calendar Products › Events Calendar PRO › PHP – If Event Has Featured Image
- This topic has 7 replies, 3 voices, and was last updated 6 years, 7 months ago by Daniel.
-
AuthorPosts
-
September 6, 2017 at 9:14 pm #1345790DanielParticipant
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?
September 7, 2017 at 5:22 pm #1346304snortonParticipantIf 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.
September 8, 2017 at 7:55 am #1346603CourtneyMemberSeptember 14, 2017 at 1:41 am #1349029DanielParticipantOk, 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.
September 14, 2017 at 1:50 am #1349036DanielParticipantNevermind. 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!
September 14, 2017 at 5:38 am #1349101CourtneyMemberThanks for the update Daniel. I’m glad it is working now. Do let us know if you need further help.
Thanks
Courtney 🙂October 6, 2017 at 9:35 am #1360372Support DroidKeymasterHey 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 -
AuthorPosts
- The topic ‘PHP – If Event Has Featured Image’ is closed to new replies.