WordPress.org

Support

Support » How-To and Troubleshooting » postimage(): is this a WP function

postimage(): is this a WP function

  • Greetings. I’m attempting to display a collection of thumbnails of the main image(s) associated with posts. The Codex details the use of the_post_thumbnail() but this depends on a picture being designated the Featured Image (in the upload dialog). My present theme uses postimage(a,b) to display the first photo inserted into a post in the dimensions chosen (a=width, b=height in pixels).

    I can’t find a reference to postimage() either in the Codex or Functions documentation. What I’d like you to help me with is whether this is a native WP function or one that may be in a plugin. The theme, BranfordMagazine by Michael Oeser does not depend on any plugins for its basic use and I haven’t installed any of the plugins that google says use a function called postimage().

    So again, is postimage() located within WP or is it deprecated? It works in the theme I’m trying to develop but so far the pages depend on the basic scripting of BranfordMagazine. Thanks for your help.

Viewing 15 replies - 1 through 15 (of 23 total)
  • esmi

    @esmi

    Forum Moderator

    postimage() is not – and has never been – part of WordPress core, so it must be a custom function in your theme.

    Thanks esmi. Seems it’s really my bad. I just looked again in functions.php and there it is right before my eyes. I could kick me.

    @esmi, while I’m on the subject of post image – could you please point me in the direction of info using the_post_thumbnail() without having to select a featured image during upload?

    I’d like to just get the main image and have its thumbnail to work with. The problem is that my blog has more than a thousand posts, nearly all with images but probably two with Featured Image applied.

    How do I directly use wp_get_attachment_image_src() and manipulate it with whatever the_post_thumbnail() uses? This would eliminate bloat via the need to use timthumb.php, which is the script postimage() uses.

    Thanks.

    esmi

    @esmi

    Forum Moderator

    Can you first define the the main image? Will it always be the first image uploaded to that Post or Page?

    Yes, esmi. It’ll always be the first image. With the first image, I always try to maintain the position, size and alignment. Keeps things constant.

    esmi

    @esmi

    Forum Moderator

    Something like:

    <?php
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
    $attachments = get_posts($args);
    if ($attachments) :
    foreach ( $attachments as $attachment ) {
    	if( wp_attachment_is_image( $attachment->ID ) ) {
    		echo wp_get_attachment_image( $attachment->ID, 'thumbnail');
    		break;
    	}
    }
    endif;?>

    should work.

    Thanks esmi. I used the code as a function fetchthumb(), called it and got the same image repeated with all the posts. Does this variable get a value from somewhere?: $post->ID

    Here’s what I have in the template’s index.php with the call to postimage() commented out and :

    [42 lines of code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    The output should be 5 rows of four thumbs with titles under each.

    esmi

    @esmi

    Forum Moderator

    If you are using the code I gave above as a function, you will either need to pass the post id to the function (eg fetchthumb( $post->ID)) or declare global $post; at the top of the function.

    Thanks a million esmi. It worked perfectly. Two more questions:

    (1) Can I set the dimensions of the thumbs independently of the Media page in Settings? Again the codex seems to suggest otherwise.

    (2) I spent the night trying to substitute an empty image for posts with no image uploaded. This is what I have but it breaks the divs and the placeholder (empty.gif) doesn’t show:


    <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>" ><?php $athumb = fetchthumb();?><?php if (empty($athumb)) { echo '<img src="'.$scriptpath.'/images/empty.gif" alt="this post has no image" />';} else { echo '$athumb';}?></a>

    I also used if isset with no luck – just throws the images to the screen with no errors or distortions. You may detect that I’m more a blogger than a coder, but I try.

    Thanks.

    esmi

    @esmi

    Forum Moderator

    1, Not unless you add a new image size to your theme’s functions.php file. See http://codex.wordpress.org/Function_Reference/add_image_size.

    2. You might be better off handling this within the fetchthumb() function itself. Drop a copy of the function (as it exists currently) into the WordPress pastebin and post the pastebin url here.

    Here’s the function with my attempt to check if empty:

    // Function to get wordpress image attachment
    function fetchthumb() {
    global $post;
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
    $attachments = get_posts($args);
    if ($attachments) :
    foreach ( $attachments as $attachment ) {
    if( wp_attachment_is_image( empty($attachment->ID) ) )
    echo '<img src="'.$scriptpath.'/images/empty.gif" alt="this post has no image" />';
    else echo wp_get_attachment_image( $attachment->ID, 'thumbnail');
    break;
    }
    endif;
    }

    The link if above isn’t visible http://pastebin.com/hdTWwS2U

    esmi

    @esmi

    Forum Moderator

    Try:

    // Function to get wordpress image attachment
    function fetchthumb() {
    global $post;
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
    $attachments = get_posts($args);
    if ($attachments) :
    foreach ( $attachments as $attachment ) {
    	if( wp_attachment_is_image( $attachment->ID ) ) echo wp_get_attachment_image( $attachment->ID, 'thumbnail');
    	else echo '<img src="' . get_stylesheet_directory_uri() . '/images/empty.gif" alt="this post has no image" />';
    	break;
    }
    endif;
    }

    This assumes that your empty.gif file is in the theme’s /images folder.

    (1) Almost there with the placeholder image, thanks. It shows once. I declared $attachment global but no change in output.

    (2) I had seen http://codex.wordpress.org/Function_Reference/add_image_size and wondered whether it depended on a Featured Image. In this function we’re fetching the attachment image

    esmi

    @esmi

    Forum Moderator

    It does depend on the featured image but it does mean that you can call a thumb of a custom pre-set size using the_post_thumbnail().

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘postimage(): is this a WP function’ is closed to new replies.
Skip to toolbar