WordPress.org

Ready to get started?Download WordPress

Forums

Images next to excerpts (1 post)

  1. storpappa
    Member
    Posted 5 years ago #

    I am trying to drop in images on the excerpts and single pages. And before anyone says there is a plugin that does this, I am trying to code it out myself. Thanks anyway though.

    Here is what I am trying to do -
    Check for an attached file - if it is there, use that
    Otherwise check for a custom field image
    Otherwise check for a custom field thumbnails
    and as a last resort use a default image, which once I get this working right, would be changed to pull the default images by the category ID.

    I can get the images if they are attached to the post as an original attachment. But if they are already in the media gallery and added to the post they do not show up. I must be calling them wrong to cover both options, or need to make 2 different calls.

    I am messing something up on the else for a default image.

    Lastly, I know this code isn't tight or pretty - I figured I'd work on that after I got it working, but any and all suggestions to improve on this are appreciated.

    <div id="postimage">
    	<?php $image0="http://stpatricksdayparties.com/wp-content/avatars/default.jpg"; ?>
    	<?php global $wpdb; $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1"); ?>
    	<?php $image1 = get_post_meta($post->ID, 'Image', $single = true); ?>
    	<?php $image2 = wp_get_attachment_url($attachment_id); ?>
    	<?php if ($image1 !== '') $image0=$image1; ?>
    	<?php if ($image2 !== '') $image0=$image2; ?>
    	<!--determine which image to use-->
    	<a href="<?php the_permalink() ?>" class="Tips2" title="Read More... :: Click here to read the rest of this entry.">
    		<img src="<?php echo $image0; ?>" alt="<?php the_title(); ?>" />;
    	</a><!--display the image-->

Topic Closed

This topic has been closed to new replies.

About this Topic