WordPress.org

Ready to get started?Download WordPress

Forums

extract image size and width for widget default image (1 post)

  1. David Laietta
    Member
    Posted 2 years ago #

    I am probably missing something obvious (happens to me all the time), but I can't use the selected image size in a post widget that I've written to resize a default image if no featured image exists. Basically, I have it first pulling the featured thumbnail of a specified size. If no featured thumbnail exists, it pulls the first image from the post. If neither of those exists, it displays a "no image found" default image, so as not to break layouts. Relevant code follows:

    This is for the dropdown in the widget options panel, and it displays the name of the image size, as well as the width and height of that size (so it outputs something like "featured-image (100 x 200).

    <?php
    			global $_wp_additional_image_sizes;
                $sizes = $_wp_additional_image_sizes;
    			?>
                <select id="<?php echo $this->get_field_id('image_size'); ?>" name="<?php echo $this->get_field_name('image_size'); ?>" class="widefat" style="width:100%;">
                <?php
                foreach ( $sizes as $key => $value ) : ?>
                    <option value="<?php echo $key ?>" <?php if ($key == $instance['image_size']) echo 'selected="selected"'; ?>>
    				<?php echo ucwords(preg_replace('/-/', ' ', $key))  . ' (' . $value['width'] . 'x' . $value['height'] . ')' ?>
                    </option>
                <?php endforeach; ?>
                </select>

    Clearly that bit works, so how do I get that to display up in the actual widget output itself?

    <?php
                            if(has_post_thumbnail()):
                                echo get_the_post_thumbnail($post->ID, $image_size, array('class' => $image_align));
                            else:
                                echo default_main_image($image_align);
    					    endif;
                            ?>

    The function default_main_image:

    <?php // Call First Uploaded Image in Post
    function default_main_image($image_align) {
    	$files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image&order=desc');
    	$the_title = get_the_title();
    
    	if($files) :
    		$keys = array_reverse(array_keys($files));
    		$num = $keys[0];
    		$image = wp_get_attachment_image($num);
    		$imagepieces = explode('"', $image);
    		$imagepath = $imagepieces[1];
    		$main = wp_get_attachment_url($num);
    		$template = get_template_directory();
    	else: //If No Image
    		$main = esc_url(get_bloginfo('template_directory')) . '/images/thumbnail.png';
    	endif;
    
    	print "<img src='$main' alt='$the_title' title='$the_title' class='$image_align wp-post-image' />";
    } ?>

    Whenever I attempt to just output the $image_size[width] or the like, I only get the first letter of the image type returned.

Topic Closed

This topic has been closed to new replies.

About this Topic