WordPress.org

Ready to get started?Download WordPress

Forums

Conditional code help need! - 2 thumbnails showing (2 posts)

  1. pharmTek
    Member
    Posted 4 years ago #

    FYI - I think I understand how to fix my problem but am only code at editing existing code and could really use some help writing this fix.

    So here's the problem... I now have 2 thumbnails showing on my single post page... why?

    Because I've added the following code to my functions.php:

    //check attachment
    function checkimage($size=medium) {
    	if ( $images = get_children(array(
    		'post_parent' => get_the_ID(),
    		'post_type' => 'attachment',
    		'numberposts' => 1,
    		'post_mime_type' => 'image',)))
    	{
    		foreach( $images as $image ) {
    			$attachmentimage=wp_get_attachment_image( $image->ID, $size );
    			return $attachmentimage;
    		}
    	}
    }
    
    //get attachment
    function postimage($size=medium) {
    	if ( $images = get_children(array(
    		'post_parent' => get_the_ID(),
    		'post_type' => 'attachment',
    		'numberposts' => 1,
    		'order' => 'ASC',
    		'post_mime_type' => 'image',)))
    	{
    		foreach( $images as $image ) {
    			$attachmentimage=wp_get_attachment_image( $image->ID, $size );
    			echo $attachmentimage.apply_filters('the_title', $parent->post_title);
    		}
    	}
    }

    With corresponding code in my single.php file to show the image attached to each post:

    <?php if(checkimage()) { // if there is an image ?>
    <?php postimages('medium'); ?>
    				<?php } ?>

    (which have been uploaded via the default media gallery)

    This all works great except for in the case when I need to use a custom field key to embed a thumbnail (which is necessary for one particular page due to the styling of those posts).

    What I need now is some conditional code to fix the problem... I'm thinking it should be something like:

    Checkimage
    IF there is a thumbnail THEN post thumbnail and END
    ELSE postimage

    Does that sound right? and if so... what's the code for that?

  2. pharmTek
    Member
    Posted 4 years ago #

    Ok, so I solved my problem a round about way...
    Created a post template for the posts that need the thumbnail (which are grouped by category).

    However, if anyone is interested in humoring me about this code, i'd love to see the solution. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic