Set Default Featured Image if Not Specified Manually

Home Forums Calendar Products Events Calendar PRO Set Default Featured Image if Not Specified Manually

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1345994
    snorton
    Participant

    Howdy folks,

    I understand you guys can’t provide much support for custom coding, and while I’m not suggesting that this be a feature request, I was wondering if you could point me in the right direction.

    I’m developing a site where I want a default featured image set for a specific category if it is not set manually. My end user will be adding events to a category with the ID ’21’ and I don’t expect that they will set a featured image, so I want a fallback.

    Now, I understand how to display a default image if one is not specified, but I actually want to SET the image so that it is called in other areas throughout the website where featured images are displayed. I have a similar function that works for regular post_types, but it isn’t working for tribe_events. Can you tell me where I’m wrong here?

    // Featured Image for Tribe Category 21
    function default_cat21_featured_image() {
    	global $post;
    	if(tribe_event_in_category( '21' ) ) {
    		$featured_image_exists = has_post_thumbnail( $event_id );
    		if ( !$featured_image_exists )  {
    			$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
    			if ( $attached_image ) {
    				foreach ( $attached_image as $attachment_id => $attachment ) {
    				set_post_thumbnail( $event_id, $attachment );
    				}
    			}
    	else 	set_post_thumbnail( $event_id, '1504' );
    		wp_reset_postdata();
         	}
    	}
    }
    add_action( 'the_post', 'default_cat21_featured_image' );
    #1347715
    Victor
    Keymaster

    Hi Sean!

    Thanks for reaching out to us! Let me try to help you with that.

    From what I can see, you are not declaring the variable $event_id, so you might want to try adding $event_id = $post->ID after global $post;

    There is a plugin that you might want to try out for a similar functionality https://wordpress.org/plugins/default-featured-image/

    I hope it helps! Is there anything else I can do for you? Let me know.

    Best,
    Victor

    #1347751
    snorton
    Participant

    Hi Victor,

    Thank you for responding. Unfortunately that makes no difference. As mentioned, this function works just fine for regular post_types but isn’t for tribe_events. I modified the code a bit to first check if the post is a tribe_event, but still no luck. I’d rather not add another plugin to use as it doesn’t seem to be efficient for me for this specific scenario.

    function default_cat21_featured_image() {
    	global $post;
    	$event_id = $post->ID;
    		if( tribe_is_event( $event_id ) ) {
    			$featured_image_exists = has_post_thumbnail( $event_id );
    			if(tribe_event_in_category( '21' ) ) {
    				if ( !$featured_image_exists )  {
    					$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
    			
    				if ( $attached_image ) {
    					foreach ( $attached_image as $attachment_id => $attachment ) {
    						set_post_thumbnail( $event_id, $attachment );
    						}
    					}
    				else set_post_thumbnail( $event_id, '1740' );
    					wp_reset_postdata();
    				}
    			}
    		}
                            
    	}
    add_action( 'the_post', 'default_cat21_featured_image' );
    #1347825
    snorton
    Participant

    Okay, because I know it works for regular post types, I’ve added a condition into that function:

    // Featured Image for Certain Post Categories
    function default_my_custom_featured_images() {
    	global $post;
    	$featured_image_exists = has_post_thumbnail($post->ID);
    	if (!$featured_image_exists)  {
    		$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
    
    	if ($attached_image) {
    		foreach ($attached_image as $attachment_id => $attachment) {
    			set_post_thumbnail($post->ID, $attachment);
    			}
    		}
    // Is this a job posting?
    	else if ( in_category( '19' ) ) {
    		set_post_thumbnail( $post->ID, '1504' );
    		wp_reset_postdata();
    			}
    // Is this a board meeting?
    	else if ( tribe_is_event( $post->ID ) ) {
    			set_post_thumbnail( $post->ID, '1740' );
    			wp_reset_postdata();
    			}
    		}
    	}
    add_action('the_post', 'default_my_custom_featured_images');

    This works! Great! But I want it to *only* work if it is in a specific category, so when I add that condition. Now there’s no result.

    // Is this a board meeting?
    	else if ( tribe_is_event( $post->ID ) && tribe_event_in_category( '21' ) ) {
    			set_post_thumbnail( $post->ID, '1740' );
    			wp_reset_postdata();
    			}
    

    I even tried just nesting the condition:

    
    // Is this a board meeting?
    	else if ( tribe_is_event( $post->ID ) ) {
    		if ( tribe_event_in_category( '21' ) ) {
    			set_post_thumbnail( $post->ID, '1740' );
    			wp_reset_postdata();
    				}
    			}
    
    #1347933
    snorton
    Participant

    I’ve resolved my inquiry by using the category slug instead of its ID. I am intrigued why that works, but since it’s working I will leave it alone.

    For anyone else who wants to set a thumbnail (aka featured image) for a specific event category when it hasn’t been manually set, here’s the solution. Add the function below to your functions.php. Again, I didn’t want to simply display a substitute image in the single-post.php template, I wanted to actually set the image programmatically so that it can be called throughout the entire site. This single function can be expanded with additional elseif statements to conditionally apply to other post types or event categories.

    // Featured Image for Certain Event Categories
    function my_default_featured_images() {
    	global $post;
    	$featured_image_exists = has_post_thumbnail($post->ID);
    	if (!$featured_image_exists)  {
    		$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
    
    	if ($attached_image) {
    		foreach ($attached_image as $attachment_id => $attachment) {
    			set_post_thumbnail($post->ID, $attachment);
    			}
    		}
    // Identify the category this should apply to.
    	else if ( tribe_is_event( $post->ID ) ) {
    		if ( tribe_event_in_category( '[INSERT CATEGORY SLUG HERE]' ) ) {
    			set_post_thumbnail( $post->ID, '0000' ); // Change '0000' to the ID of whatever image you want as a default
    			wp_reset_postdata();
    				}
    			}
    		}
    	}
    add_action('the_post', 'my_default_featured_images');
    #1347945
    Victor
    Keymaster

    Hi Sean!

    Great catch! Yes, you will need to place the category slug when using the ‘tribe_event_in_category()‘ function.

    Thanks for following up to let us know about this and for sharing the code so other users can use it.

    I’ll close this now, but feel free to open a new topic if anything comes up and we’d be happy to help 🙂

    Best,
    Victor

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Set Default Featured Image if Not Specified Manually’ is closed to new replies.