WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using Multiple Images in Post-Image Plugin (2 posts)

  1. noelgreen
    Member
    Posted 5 years ago #

    I'm using the 'post-image' plugin and am trying to get it to display multiple images. There's some "code" on the website, but when I copy and paste it it screws it all up and I'm not sure what's wrong.

    Can anyone help?

    Here's the original post-image code.

    function szub_post_image($args='') {
    	parse_str($args);
    	if (!isset($default_image))  $default_image = '';
    	if (!isset($use_thumb))  $use_thumb = false;
    	if (!isset($img_tag))  $img_tag = true;
    	if (!isset($css_class))  $css_class = 'post-image';
    	if (!isset($customkey))  $customkey = 'post-image';
    	if (!isset($display))  $display = true;
    
    	return post_image($default_image, $use_thumb, $img_tag, $css_class, $customkey, $display);
    }
    
    function post_image($default_image='', $use_thumb=false, $img_tag=true, $css_class='post-image', $customkey='post-image', $display=true) {
    	global $post, $posts, $wp_version, $wpdb;
    	global $post_image_attachments;
    
    	if (empty($post))
    		return;
    
    	if (!empty($posts))  {
    		foreach($posts as $apost) {
    			if ($posts[0] != $apost)
    				$IN_ids .= ',';
    			$IN_ids .= (int) $apost->ID;
    		}
    	}
    
    	if (!empty($default_image))  {
    		$img_url = $default_image;
    		$img_title = apply_filters('the_title', $post->post_title);
    	}
    
    	$post_custom = get_post_custom($post->ID);
    	$meta_value = $post_custom["$customkey"][0];
    
    	if ($meta_value)  {
    		$img_url = $meta_value;
    		$img_title = apply_filters('the_title', $post->post_title);
    	} else {
    		if (empty($post_image_attachments))  {
    			$record =  ($wp_version < 2.1)  ? 'post_status' : 'post_type';
    			$post_image_attachments = @$wpdb->get_results("SELECT ID, post_parent, post_title, post_content, guid FROM $wpdb->posts WHERE post_parent IN($IN_ids) AND $record = 'attachment' AND post_mime_type LIKE '%image%' ORDER BY post_date ASC");
            }
    
            $first_attachment = 1;
    
            foreach ($post_image_attachments as $attachment) {
    
                if ($post->ID == $attachment->post_parent) {
    
                    if ($first_attachment) {
                        $img_post_id = $attachment->ID;
                        $img_url = $attachment->guid;
                        $img_title = apply_filters('the_title', $attachment->post_title);
                        $first_attachment = 0;
                    }
    
    				$postmarked = strpos(strtolower($attachment->post_title), strtolower($customkey));
    				$fileimage = explode('.', basename($attachment->guid));
    
                    /* if either the image filename is equal to the post name or ID
                     * or if the custom key is found on the image title, the we
                     * use this image instead of the first one found. */
    				if ($postmarked == true || $post->ID == $fileimage[0] || $post->post_name == $fileimage[0])  {
    					$img_url = $attachment->guid;
    					$img_title = apply_filters('the_title', $attachment->post_title);
    					if($postmarked == true) {
    						$img_title = trim(str_replace($customkey, '', $img_title));
                        }
                        break;
    				}
    			}
    		}
    
            if ($use_thumb && ($img_url != $default_image)) {
    
                /* get the thumbnail filename */
                $img_meta = @$wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND post_id = ".$img_post_id.";");
                $img_meta = @unserialize(@$img_meta[0]->meta_value);
    
                if (@$img_meta['thumb']) {
                    $thumb_filename = @$img_meta['thumb'];
                } else {
                    $thumb_filename = @$img_meta['sizes']['thumbnail']['file'];
                }
    
                $img_url = substr($img_url, 0, strrpos($img_url, '/')+1).$thumb_filename;
    
            }
    
    	}
    
        $img_path = ABSPATH . str_replace(get_settings('siteurl'), '', $img_url);
    
    	if (!file_exists($img_path))  {
    		return;
        } else {
    		if ($img_tag)  {
    			$imagesize = @getimagesize($img_url);
    			$image = '<img class="' . $css_class . '" src="' . $img_url . '" ' . $imagesize[3] . ' title="' . $img_title . '" alt="' . $img_title . '" />';
    		} else {
    			$image = $img_url;
    		}
    	}
    
    	if ($display)
    		echo $image;
    
    	return $image;
    }

    And here's the hacked "multiple image" code.
    Can anyone see what it SHOULD be like? What's missing... wrong... etc.
    It may be something to do with WP 2.6 even.

    function post_image_multiple($default_image='', $use_thumb=true, $img_tag=tue, $css_class='post-image', $customkey='post-image', $display=true) {
    global $post, $posts, $wp_version, $wpdb;
    global $post_image_attachments;
    if( empty($post))
    return;
    if( !empty($posts)) {
    foreach($posts as $apost) {
    if( $posts[0] != $apost )
    $IN_ids .= ',';
    $IN_ids .= (int) $apost->ID;
    }
    }
    $post_custom = get_post_custom($post->ID);
    $meta_value = $post_custom["$customkey"][0];
    if( $meta_value ) {
    $img_url = $meta_value;
    $img_title = apply_filters('the_title', $post->post_title);
    } else {
    if( empty($post_image_attachments)) {
    $record = ( $wp_version < 2.1 ) ? 'post_status' : 'post_type';
    $post_image_attachments = @$wpdb->get_results("SELECT ID, post_parent, post_title, post_content, guid FROM $wpdb->posts WHERE post_parent IN($IN_ids) AND $record = 'attachment' AND post_mime_type LIKE '%image%' ORDER BY post_date ASC");
    }
    
    #echo sizeof($post_image_attachments);
    foreach( $post_image_attachments as $attachment ) {
    
    #echo "in foreach";
    $img_url = $attachment->guid;
    $img_title = apply_filters('the_title', $attachment->post_title);
    $postmarked = strpos(strtolower($attachment->post_title), strtolower($customkey));
    $fileimage = explode('.', basename($attachment->guid));
    if( $postmarked == true || $post->ID == $fileimage[0] || $post->post_name == $fileimage[0] ) {
    $img_url = $attachment->guid;
    $img_title = apply_filters('the_title', $attachment->post_title);
    if($postmarked == true) {
    $img_title = trim(str_replace($customkey, '', $img_title));
    break;
    }
    }
    if( $use_thumb && ($img_url != $default_image))
    $img_url = preg_replace('!(\.[^.]+)?$!', __('.thumbnail') . '$1', $img_url, 1);
    
    $img_path = ABSPATH . str_replace(get_settings('siteurl'), '', $img_url);
    if( !file_exists($img_path)) {
    return;
    } else {
    if( $img_tag ) {
    $imagesize = @getimagesize($img_url);
    $image_link_url_array = explode('/', $img_url);
    $image_link_filename = $image_link_url_array[sizeof($image_link_url_array) - 1];
    $length_thumb_name = strlen($image_link_filename);
    $image_link_filename_array = explode('.', $image_link_filename);
    $image_link_filename = $image_link_filename_array[0] . '.' . $image_link_filename_array[sizeof($image_link_filename_array) - 1];
    
    #echo $image_link_filename;
    unset($image_link_url_array[sizeof($image_link_url_array) - 1]);
    $image_link_url = substr($img_url, 0, strlen($img_url) - $length_thumb_name) . $image_link_filename;
    $image = '<a href="' . $image_link_url . '" title="' . $img_title . '" rel="lightbox[slideshow]"><img class="' . $css_class . '" src="' . $img_url . '" ' . $imagesize[3] . ' title="' . $img_title . '" alt="' . $img_title . '" /></a>';
    } else {
    $image = $img_url;
    }
    }
    if( $display )
    echo $image;
    
    # return $image;
    }
    if( !empty($default_image) && sizeof($post_image_attachments) == 0 ) {
    $img_url = $default_image;
    $img_title = apply_filters('the_title', $post->post_title);
    if( $img_tag ) {
    $imagesize = @getimagesize($img_url);
    $image = '<img class="' . $css_class . '" src="' . $img_url . '" ' . $imagesize[3] . ' title="' . $img_title . '" alt="' . $img_title . '" />';
    }
    if( $display )
    echo $image;
    }
    }
    }

    Thanks!

  2. noelgreen
    Member
    Posted 5 years ago #

    Got it!!! :)

    function post_image_multiple($default_image='', $use_thumb=false, $img_tag=true, $css_class='post-image', $customkey='post-image', $display=true) {
    	global $post, $posts, $wp_version, $wpdb;
    	global $post_image_attachments;
    
    	if (empty($post))
    		return;
    
    	if (!empty($posts))  {
    		foreach($posts as $apost) {
    			if ($posts[0] != $apost)
    				$IN_ids .= ',';
    			$IN_ids .= (int) $apost->ID;
    		}
    	}
    
    	$post_custom = get_post_custom($post->ID);
    	$meta_value = $post_custom["$customkey"][0];
    
    	if ($meta_value)  {
    		$img_url = $meta_value;
    		$img_title = apply_filters('the_title', $post->post_title);
    	} else {
    		if (empty($post_image_attachments))  {
    			$record =  ($wp_version < 2.1)  ? 'post_status' : 'post_type';
    			$post_image_attachments = @$wpdb->get_results("SELECT ID, post_parent, post_title, post_content, guid FROM $wpdb->posts WHERE post_parent IN($IN_ids) AND $record = 'attachment' AND post_mime_type LIKE '%image%' ORDER BY post_date ASC");
            }
    
    #echo sizeof($post_image_attachments);
    foreach( $post_image_attachments as $attachment ) {
    
    #echo "in foreach";
    $img_url = $attachment->guid;
    $img_title = apply_filters('the_title', $attachment->post_title);
    $postmarked = strpos(strtolower($attachment->post_title), strtolower($customkey));
    $fileimage = explode('.', basename($attachment->guid));
    if( $postmarked == true || $post->ID == $fileimage[0] || $post->post_name == $fileimage[0] ) {
    $img_url = $attachment->guid;
    $img_title = apply_filters('the_title', $attachment->post_title);
    if($postmarked == true) {
    $img_title = trim(str_replace($customkey, '', $img_title));
    break;
    }
    }
    
            if ($use_thumb && ($img_url != $default_image)) {
    
                /* get the thumbnail filename */
                $img_meta = @$wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND post_id = ".$img_post_id.";");
                $img_meta = @unserialize(@$img_meta[0]->meta_value);
    
                if (@$img_meta['thumb']) {
                    $thumb_filename = @$img_meta['thumb'];
                } else {
                    $thumb_filename = @$img_meta['sizes']['thumbnail']['file'];
                }
                $img_url = substr($img_url, 0, strrpos($img_url, '/')+1).$thumb_filename;
            }
    
    $img_path = ABSPATH . str_replace(get_settings('siteurl'), '', $img_url);
    if( !file_exists($img_path)) {
    return;
    } else {
    if( $img_tag ) {
    $imagesize = @getimagesize($img_url);
    
    $image_link_url_array = explode('/', $img_url);
    $image_link_filename = $image_link_url_array[sizeof($image_link_url_array) - 1];
    $length_thumb_name = strlen($image_link_filename);
    $image_link_filename_array = explode('.', $image_link_filename);
    $image_link_filename = $image_link_filename_array[0] . '.' . $image_link_filename_array[sizeof($image_link_filename_array) - 1];
    
    #echo $image_link_filename;
    unset($image_link_url_array[sizeof($image_link_url_array) - 1]);
    $image_link_url = substr($img_url, 0, strlen($img_url) - $length_thumb_name) . $image_link_filename;
    
    			$image = '<img class="' . $css_class . '" src="' . $img_url . '" ' . $imagesize[3] . ' title="' . $img_title . '" alt="' . $img_title . '" />';
    		} else {
    			$image = $img_url;
    		}
    	}
    
    if( $display )
    echo $image;
    
    # return $image;
    }
    if( !empty($default_image) && sizeof($post_image_attachments) == 0 ) {
    $img_url = $default_image;
    $img_title = apply_filters('the_title', $post->post_title);
    if( $img_tag ) {
    $imagesize = @getimagesize($img_url);
    $image = '<img class="' . $css_class . '" src="' . $img_url . '" ' . $imagesize[3] . ' title="' . $img_title . '" alt="' . $img_title . '" />';
    }
    if( $display )
    echo $image;
    }
    }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic