Set organizer featured image as the default event featured image

Home Forums Calendar Products Events Calendar PRO Set organizer featured image as the default event featured image

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #931158
    Marc
    Participant

    I have been trying for a while to wrap my head around setting the organizer featured images as the default event featured image. I have a lot of events, and when a specific featured image is not set on an event, I want it to default back to the organizer’s featured image. But I don’t quite know how to make that call. Here’s what I have so far:

    add_filter( 'tribe_event_featured_image', 'set_default_featured_img', 10, 4 );
    function set_default_featured_img( $featured_image, $post_id, $size, $image_src ) {
      if ( ! $featured_image ) {
            $post_id = tribe_get_organizer_id();
    	$image_src = wp_get_attachment_image_src( $post_thumbnail_id ( $post_id ), $size );
      }
      return $featured_image;
    }

    I was thinking the following: 1) If no featured image found on the event 2) look for the organizer post id and grab the featured image from that post id 3) return the organizer’s featured image.

    But this function doesn’t work as intended. I know it’s doable, I’m just stuck.

    #931514
    Josh
    Participant

    Hey Marc,

    Thanks for reaching out to us! I made a couple of quick modifications to the snippet you provided. I haven’t tested it but it should be enough to help you get the customization you’re looking for live.

    
    add_filter( 'tribe_event_featured_image', 'set_default_featured_img', 10, 4 );
    
    function set_default_featured_img( $featured_image, $post_id, $size, $image_src ) {
    if ( ! $featured_image ) {
    $post_id = tribe_get_organizer_id();
    $image_src = wp_get_attachment_image_src( $post_thumbnail_id ( $post_id ), $size );
    return $image_src;
    } else {
    return $featured_image;
    }
    

    Let me know if this helps.

    Thanks!

    #931847
    Marc
    Participant

    I found a solution, but it seems rather cumbersome. I would think a cleaner solution would be possible using WP’s native thumbnail functions, but I can’t figure it out.

    When I use the “tribe_event_featured_image” function, it replaces the images well wherever that function is loaded in the templates. However, I also use for example the Genesis Framework and their Responsive Slider plugin. The solution with the tribe function didn’t populate the slider with the featured image, as that was called with the “genesis_image” function (which uses and filters WP’s thumbnail function).

    That’s why I think a function that deals with the native capabilities would be better too. But here’s the code that made it possible for me to display the organizer’s featured image as the event’s featured image if no featured image was set.

    /** Define a default post thumbnail Genesis */
    function default_event_image_fallback($output, $args) {
        global $post;
        if( $output || $args['size'] == 'full' && get_post_type() == 'tribe-events' )
            return $output;
     
     	$post_id = tribe_get_organizer_id();
    	$image_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
        $thumbnail = $image_src[0];
     
        switch($args['format']) {
     
            case 'html' :
                return '<img src="'.$thumbnail.'" class="attachment-'. $args['size'] .'" alt="'. get_the_title($post_id) .'" />';
                break;
            case 'url' :
                return $thumbnail;
                break;
           default :
               return $output;
                break;
        }
    }
    add_filter('genesis_get_image', 'default_event_image_fallback', 10, 2);
    
    /** Define a default post thumbnail Events Calendar */
    function set_default_event_featured_img( $featured_image, $post_id, $size, $image_src ) {
    	if ( ! $featured_image ) {
    		$post_id = tribe_get_organizer_id();
    		$image_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
    		$featured_image = '';
    
    		//if link is not specifically excluded, then include <a>
    		if ( ! empty( $image_src ) && $link ) {
    			$featured_image .= '<div class="tribe-events-event-image"><a href="' . tribe_get_event_link() . '" title="' . get_the_title( $post_id ) . '"><img src="' . $image_src[0] . '" title="' . get_the_title( $post_id ) . '" /></a></div>';
    		} elseif ( ! empty( $image_src ) ) {
    			$featured_image .= '<div class="tribe-events-event-image"><img src="' . $image_src[0] . '" title="' . get_the_title( $post_id ) . '" /></div>';
    		}
    
    		return apply_filters( 'tribe_event_featured_image', $featured_image, $post_id, $size, $image_src );
    	} 
    	else {
    		return $featured_image;
    	}
    }
    add_filter( 'tribe_event_featured_image', 'set_default_event_featured_img', 10, 4 );
    #933068
    Josh
    Participant

    Hey,

    Thanks for following up!

    It can be a little tricky with multiple places using different custom functions for accessing the post thumbnail. I did a little research and it looks like this article might be a nifty solution for what you’re trying to accomplish. However, rather than using an attached image as the default you can use the organizer queries like you’ve done above.

    This way rather than filtering two functions specific to two different scenarios, you’re directly modifying the core image that both functions should be using.

    Let me know if this helps.

    Thanks1

    #983675
    Support Droid
    Keymaster

    This topic has not been active for quite some time and will now be closed.

    If you still need assistance please simply open a new topic (linking to this one if necessary)
    and one of the team will be only too happy to help.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Set organizer featured image as the default event featured image’ is closed to new replies.