• I’m looking for a way for WP to save the image code of the first image attachment into the excerpt field when a post is published. Any idea how I could accomplish this?

    So basically the excerpt field would contain something like:

    <img src="http://testing.com/wp-content/uploads/2009/09/EastBeach21-50x50.jpg" width="50" height="50" alt="Test" title="Test" />

    upon publishing the post.

    I have this code that does it when you hit save draft, but I’m using a combination of Postie and DagonDesigns draft schedule plugin, and need it to add it to the excerpt without needing to hit save draft, since the scheduler will publish automatically. This code is a little buggy too btw

    function pb_insert_excerpt(){
    	$post_data = &$_POST;
    	$post_id = $post_data['ID'] ;
    	$post_title = $post_data['post_title'];
    	$post_excerpt = $post_data['post_excerpt'];
    	$arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post_id);
    	if($arrImages) {
    		$arrKeys = array_keys($arrImages);
    		$iNum = $arrKeys[0];
    		$sThumbUrl = wp_get_attachment_thumb_url($iNum);
    		$thumbWidth = get_option("thumbnail_size_w");
    		$thumbHeight = get_option("thumbnail_size_h");
    		$sImgString = '<img src="' . $sThumbUrl . '" width="'.$thumbWidth.'" height="'.$thumbHeight.'" alt="'.$post_title.'" title="'.$post_title.'" />' ;
    		$existing_img = strstr($post_excerpt, 'jpg');
    		if($post_data['post_excerpt'] = isset($post_data['excerpt'])) {
    			if ($existing_img) {
    				return $post_excerpt;
    			} else {
    				return $sImgString;
    			}
    		}
    	}
    }

    Thank you

Viewing 1 replies (of 1 total)
  • Thread Starter shaneholden

    (@shaneholden)

    Forgot to include the rest of the code in the functions file for the excerpt.

    add_filter('excerpt_save_pre', 'pb_insert_excerpt');
    remove_filter('the_excerpt', 'wpautop');
Viewing 1 replies (of 1 total)
  • The topic ‘Force thumbnail of first image attachment into excerpt’ is closed to new replies.