• Resolved haltas

    (@haltas)


    I′ve been running a website for a long time and want to use the leaf theme now. By the way i think it′s a great theme. But here is my problem:
    Since there is a huge amount of posts published already, it is not possible for me to go back in all of them and give them featured images. Is there a way to take the first image inside the post and use it as featured too?
    I′ve already developed s solution for posts containing no single image by letting them use a “category-image”. But i also would like to have posts containing images or one image displaying their first image as the featured one. So far, i got this:

    if (!function_exists('leaf_get_post_image')):
    
    	function leaf_get_post_image($image_id = null, $post_id = null, $use_attachments = false, $url = null, $size = 'large') {
    		global $id,$blog_id;
    		$thumbnail_id = get_post_thumbnail_id();
    		$post_id = ( $post_id == null ) ? $id : $post_id;
    		$attachment = array();
    		<em>$category = get_the_category();</em>
    
    		// If a URL is specified, use that.
    		if ($url)
    			return $url;
    
    		// If image_id is specified, use that.
    		elseif ($image_id)
    			$attachment = wp_get_attachment_image_src( $image_id, $size );
    
    		// Check to see if NextGen Gallery is present.
    		elseif(stripos($thumbnail_id,'ngg-') !== false && class_exists('nggdb')){
    			$nggImage = nggdb::find_image(str_replace('ngg-','',$thumbnail_id));
    			$attachment = array(
    				$nggImage->imageURL,
    				$nggImage->width,
    				$nggImage->height
    			);
    		}
    
    		// If not, let's use the post's featured image.
    		elseif ( has_post_thumbnail( $post_id) )
    			$attachment = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
    
    		// Otherwise, and only if we want to, just use the last image attached to the post.
    		elseif ( $use_attachments == true ) {
    			$images = get_children( array(
    				'post_parent' => $post_id,
    				'post_type' => 'attachment',
    				'numberposts' => 1,
    				'post_mime_type' => 'image'));
    			foreach($images as $image) { $attachment = wp_get_attachment_image_src( $image->ID, $size ); }
    		}
    
    		// If there is no image, use the default image (available filter: leaf_post_image_none).
    
    		if (isset($attachment[0])) {
    			$post_image_uri = $attachment[0];
    		} <em>elseif ( 'slider' == $size ) {
    			if (isset($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/' . $category[1]->cat_name . '.jpg' );
    			}
    			elseif (empty($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/' . $category[0]->cat_name . '.jpg' );
    			}
    			else {
    			$post_image_uri = apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/keinbild.jpg' );
    			}
    		} else {
    			if (isset($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/' . $category[1]->cat_name . 'klein.jpg' );
    			}
    			elseif (empty($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/' . $category[0]->cat_name . 'klein.jpg' );
    			}
    			else {
    			$post_image_uri = apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/keinbildklein.jpg' );
    			}
    		}</em>
    		// If no image, return now
    		if ( $post_image_uri == apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/no-image.jpg' ) || apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/no-image-small.jpg' ) )
    			return $post_image_uri;
    
    		// If MU/MS install, we need to dig a little deeper and link through the blogs.dir.
    		if ('IS_MU') {
    			$imageParts = explode('/files/', $post_image_uri);
    			if (isset($imageParts[1])) {
    				$post_image_uri = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
    			}
    		}
    
    		return $post_image_uri;
    	}
    endif;

    Even doing these adjustments was really hard for me (first time using or reading php). So right now I′m more than clueless.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter haltas

    (@haltas)

    those “<em>“s should mark the lines I changed or edited ??

    Thread Starter haltas

    (@haltas)

    Made it! Set $use_attachments in line 3 to ‘true’ and the function will give you the last image attached to the post. If you want to have the first image attached to the post (in most cases, I assume, the one standing nearest to the top) just add 'order' => 'ASC' like this:

    // Otherwise, and only if we want to, just use the last image attached to the post.
    		elseif ( $use_attachments == true ) {
    			$images = get_children( array(
    				'post_parent' => $post_id,
    				'post_type' => 'attachment',
    				'numberposts' => 1,
    				'order' => 'ASC',
    				'post_mime_type' => 'image'));
    			foreach($images as $image) { $attachment = wp_get_attachment_image_src( $image->ID, $size ); }
    		}

    It will have the array beginning with the last post and turn the returning variable $attachement into the source of first image attached to the post.

    Hi, I’m thinking of changing the theme, but I have more than 200 blog posts (with photos, of course). I’d like to use your code, but I don’t know where to copy-paste it.

    Also, it seems that for featured photos Leaf only uses photos that were uploaded to the server. In my case, the majority of photos were linked from my Flickr profile and I can’t choose those photos in the Set Featured Image. Do you have any idea how to select those photos?

    Thread Starter haltas

    (@haltas)

    I′ll be on that one. For the meantime you can use this:
    Read carefully: If you don′t know how to work with your ftp-files, do none of the changes shown. Even if this code works for me, it can cause problems with your site you only can solve through ftp by replacing the changed code again with the original lines. Be sure you copy and save the original code before you do anything. Having a dummy site for testing is helpful when performing major changes like this.

    if (!function_exists('leaf_get_post_image')):
    
    	function leaf_get_post_image($image_id = null, $post_id = null, $use_attachments = true, $url = null, $size = 'large') {
    		global $id,$blog_id;
    		$thumbnail_id = get_post_thumbnail_id();
    		$post_id = ( $post_id == null ) ? $id : $post_id;
    		$attachment = array();
    		$category = get_the_category();
    
    		// If a URL is specified, use that.
    		if ($url)
    			return $url;
    
    		// If image_id is specified, use that.
    		elseif ($image_id)
    			$attachment = wp_get_attachment_image_src( $image_id, $size );
    
    		// Check to see if NextGen Gallery is present.
    		elseif(stripos($thumbnail_id,'ngg-') !== false && class_exists('nggdb')){
    			$nggImage = nggdb::find_image(str_replace('ngg-','',$thumbnail_id));
    			$attachment = array(
    				$nggImage->imageURL,
    				$nggImage->width,
    				$nggImage->height
    			);
    		}
    
    		// If not, let's use the post's featured image.
    		elseif ( has_post_thumbnail( $post_id) )
    			$attachment = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
    
    		// Otherwise, and only if we want to, just use the last image attached to the post.
    		elseif ( $use_attachments == true ) {
    			$images = get_children( array(
    				'post_parent' => $post_id,
    				'post_type' => 'attachment',
    				'numberposts' => 1,
    				'order' => 'ASC',
    				'post_mime_type' => 'image'));
    			foreach($images as $image) { $attachment = wp_get_attachment_image_src( $image->ID, $size ); }
    		}
    
    		// If there is no image, use the default image (available filter: leaf_post_image_none).
    
    		if (isset($attachment[0])) {
    			$post_image_uri = $attachment[0];
    		} elseif ( 'slider' == $size ) {
    			if (isset($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/' . $category[1]->cat_name . '.jpg' );
    			}
    			elseif (empty($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/' . $category[0]->cat_name . '.jpg' );
    			}
    			else {
    			$post_image_uri = apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/keinbild.jpg' );
    			}
    		} else {
    			if (isset($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/' . $category[1]->cat_name . 'klein.jpg' );
    			}
    			elseif (empty($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/' . $category[0]->cat_name . 'klein.jpg' );
    			}
    			else {
    			$post_image_uri = apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/keinbildklein.jpg' );
    			}
    		}
    		// If no image, return now
    		if ( $post_image_uri == apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/no-image.jpg' ) || apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/no-image-small.jpg' ) )
    			return $post_image_uri;
    
    		// If MU/MS install, we need to dig a little deeper and link through the blogs.dir.
    		if ('IS_MU') {
    			$imageParts = explode('/files/', $post_image_uri);
    			if (isset($imageParts[1])) {
    				$post_image_uri = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
    			}
    		}
    
    		return $post_image_uri;
    	}
    endif;

    Be sure you copy the original code before you paste this one into the theme-plugins.php, which you can edit within your wordpress admin view. If you want to use those lines including $category etc. you will have to put images into …/wp-content/themes/leaf/images via ftp named exactly after your categories(.jpg) for the slider (see

    elseif ( 'slider' == $size ) {
    			if (isset($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/' . $category[1]->cat_name . '.jpg' );
    			}
    			elseif (empty($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/' . $category[0]->cat_name . '.jpg' );
    			}
    			else {
    			$post_image_uri = apply_filters( 'leaf_slider_image_none', get_template_directory_uri() . '/images/keinbild.jpg' );
    			}

    )
    or ‘categories-exact-name-here’klein.jpg for the thumbnails in this part:

    else {
    			if (isset($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/' . $category[1]->cat_name . 'klein.jpg' );
    			}
    			elseif (empty($category[1]->cat_name)) {
    			$post_image_uri = apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/' . $category[0]->cat_name . 'klein.jpg' );
    			}
    			else {
    			$post_image_uri = apply_filters( 'leaf_post_image_none', get_template_directory_uri() . '/images/keinbildklein.jpg' );
    			}
    		}

    Btw ‘klein’ (german) means small. Images’ sizes should be 720x320px for the slider and 300x200px (I′m not sure) for those named ‘categories-exact-name-here’klein.jpg . As you may notice, there also are ‘keinbild.jpg’ (means ‘noimage’) and ‘keinbildklein.jpg’, keinbild.jpg should be 720×320 and keinbildklein.jpg 300x200px. In case none of the category-images can be used, those will be shown. You could use your logo or something like that for them. Don′t forget to have those in your …/wp-content/themes/leaf/images , too. Maybe you are wondering why there is $category[0]->cat_name and $category[1]->cat_name . It has the function to take the second category linked to the post if there are more than one categories (e.g. sub-categories). $category[0]->cat_name is the first category and $category[1]->cat_name is the second. If there are any questions, ask them before doing anything ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to pull image out of post in leaf_get_post_image’ is closed to new replies.