0

I have a bit of dilemma regarding featured image. Here's my situation. I'm currently using featured image tied to a post to display on the home page, and on the single post page as well.

However I've run into an issue where I'm using a featured image, but the post also has an image, in this case, I don't want to show the featured image on the post page, but only the image that is in the post.

I still require the feature image for the home page (as it's a different image then the post page image).

Does that make sense?

Bottom line -all post require a feature image -feature image used in homepage, and on single post page -however if post has an image don't display the feature image

Any help?

  • So you're looking for code logic that grabs the first image of the post and displays that instead of the featured image? What have you tried so far? – Howdy_McGee Nov 19 '15 at 22:46
0

on the template of the page that you are trying to pull the first image on,(if its the post itself, then single.php) you could use the following code instead of the_post_thumbnail

function echo_first_image( $postID ) {
$args = array(
    'numberposts' => 1,
    'order' => 'ASC',
    'post_mime_type' => 'image',
    'post_parent' => $postID,
    'post_status' => null,
    'post_type' => 'attachment',
);

$attachments = get_children( $args );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' )  ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );

        echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="current">';
    }
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.