WordPress.org

Ready to get started?Download WordPress

Forums

postimage(): is this a WP function (24 posts)

  1. starapple
    Member
    Posted 2 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

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

  3. starapple
    Member
    Posted 2 years ago #

    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.

  4. starapple
    Member
    Posted 2 years ago #

    @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.

  5. esmi
    Forum Moderator
    Posted 2 years ago #

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

  6. starapple
    Member
    Posted 2 years ago #

    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.

  7. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  8. starapple
    Member
    Posted 2 years ago #

    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.

  9. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  10. starapple
    Member
    Posted 2 years ago #

    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.

  11. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  12. starapple
    Member
    Posted 2 years ago #

    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;
    }
  13. starapple
    Member
    Posted 2 years ago #

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

  14. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  15. starapple
    Member
    Posted 2 years ago #

    (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

  16. esmi
    Forum Moderator
    Posted 2 years ago #

    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().

  17. starapple
    Member
    Posted 2 years ago #

    True esmi, but remember I have about 1/1000 featured images, which is why you've helped me to fetch the attached image. BTW, still trying to figure out why the placeholder empty.gif doesn't seem to loop. Itjust prints to the screen once in the above function. Thanks for all your help so far. You contribute so much daily.

  18. esmi
    Forum Moderator
    Posted 2 years ago #

    Is the url correct for the empty.gif file on the front end pages?

  19. starapple
    Member
    Posted 2 years ago #

    Mornin. Yes, the image shows in the first instance where there's no photo with a post but doesn't show after. Here's a link to the contents of index.php: http://pastebin.com/QLcGaA4A

  20. esmi
    Forum Moderator
    Posted 2 years ago #

    Any chance of a link to a page demonstrating the problem?

  21. starapple
    Member
    Posted 2 years ago #

    Unfortunately, I'm doing the test on a live site and viewing the changes in the theme manager. If I set up a local version with a dynamic domain the posts and images wouldn't be available. If WP has a way to use two themes simultaneously I could do that a post a link.

  22. esmi
    Forum Moderator
    Posted 2 years ago #

    Unfortunately, there isn't an option to run 2 themes on one site easily without an admin login. Can you drop the output from the page into the pastebin so I can have a look at it? Just the part that includes the output from the fetchthumb() function?

  23. starapple
    Member
    Posted 2 years ago #

    Found a plugin that does the trick: http://www.abengnews.com/?themedemo=abengMobile2

  24. starapple
    Member
    Posted 2 years ago #

    Hi esmi, did you get a chance to look at the page: http://www.abengnews.com/?themedemo=abengMobile2

    I tried using a background image in css, which worked if all backgrounds were white or clear but affects the design when background colours are added.

Topic Closed

This topic has been closed to new replies.

About this Topic