how to pull image out of post in leaf_get_post_image
-
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.
- The topic ‘how to pull image out of post in leaf_get_post_image’ is closed to new replies.