0

The following function looks for the Wordpress featured image and if the post does not have one, it searches for the first attachment image to the post and uses that instead.

It executes perfectly when in PHP on the page, however when I try to use it as a function that can be called the second part inside the ELSE section is not working on some posts.

Ive checked it with an SQL query and there is no reason why it should not work.

function title_image($size,$post_id,$class){



if(has_post_thumbnail($post_id)){
            $return = get_the_post_thumbnail($post_id,$size,array('class' => $class));
        }
        else {
            //Section not working on all posts //
            $imgs = get_posts(array('post_type' => 'attachment', 'numberposts' => 1, 'post_parent' => $post_id));
            foreach($imgs as $img){
                $return = '<img src="'.$img->guid.'" class="'.$class.'" />';
            }
        }
        return $return;
}

Called on page like this:

echo title_image('full',get_the_ID(),'featuredimg');

Why does this work when placed on the page but not when called as a function

3
  • Your foreach loop would only return the LAST featured image, since you're doing a straight = assignment in there. You say only want one, so this is technically correct. But if you end up having lots of images attached to a post, you'll be wasting a lot of cycles building/throwing away strings. Consider using end() to simply jump to the end of the array and current() to get that particular element. Commented Nov 25, 2013 at 14:50
  • Only thing I can think of (however highly unlikely) is that the get_posts function is not global. Try setting $return at the top of your function to something like false so that you can see if it is even trying to set it... Commented Nov 25, 2013 at 14:51
  • @MarcB thats more efficient thanks, any ideas on whats causing the problem though Commented Nov 25, 2013 at 15:00

2 Answers 2

1

in the get_posts args, set the post status and the mime type:

'post_status' => 'inherit',
'post_mime_type' => 'image'

Also, you shouldn't use the guid, get the attachment src with wp_get_attachment_image_src():

foreach( $imgs as $img ){
    $thumb = wp_get_attachment_image_src( $img->ID, $size, false );
    $return = '<img src="' . $thumb[0] . '" class="' . $class . '" />';
}

I'd recommend to use get_children to retrieve attachments, instead of get_posts:

http://codex.wordpress.org/Function_Reference/get_children#Show_the_first_image_associated_with_the_post

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but that is still returning no array for certain posts with no explanation why.
0

Still no explanation why, but the problem is with the has_post_thumbnail() function.

This was returning true for some posts for a reason I cannot explain as they do not have featured images.

The code is modified to get around this:

function title_image($size,$postid,$class){


    $return = get_the_post_thumbnail($postid,$size,array('class' => $class));

    if(!$return) {
        $imgs = get_children(array('post_type' => 'attachment', 'numberposts' => 1, 'post_parent' => $postid));
        foreach($imgs as $img){
            $return = '<img src="'.$img->guid.'" class="'.$class.'" />';
        }
    }
    return $return;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.