Jess McKenzie
Jess McKenzie

Reputation: 8385

Wordpress get_post_thumbnail_id not returning anything

Why is it when I use wp_get_attachment_image_src(get_post_thumbnail_id($post_id)); I get nothing however when I var_dump the post_id I get string(3) "652" so I know that there is a ID

Without (string) it gives me int(652)

Code:

<?php
    $getPosts = new WP_Query(array('showposts' => 5, 'orderby' => 1));

     if($getPosts->have_posts()):
          $firstPost = true;

          while($getPosts->have_posts()):
                $getPosts->the_post();

              $cssClass = '';

              if($firstPost)
              {
                $cssClass = array('article','first-post');
                $postValue  = get_the_ID();
                $post_id    = (string)$postValue;
              }else{
                $cssClass = array('article');
              }
?>

<?php if($firstPost): ?>
    <?php $image =  wp_get_attachment_image_src(get_post_thumbnail_id($post_id));?>
    <a href="<?php echo $image[0];?>">Test</a>

Upvotes: 0

Views: 1772

Answers (1)

Giacomo1968
Giacomo1968

Reputation: 26014

It seems like you are using wp_get_attachment_image_src incorrectly as explained in the WordPress documentation:

Returns an ordered array with values corresponding to the (0) url, (1) width, (2) height, and (3) scale of an image attachment (or an icon representing any attachment).

So it seems like your code should be as follows:

<?php if($firstPost): ?>
<?php
  $image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id($post_id));
  $image = $image_attributes[0];
?>
<a href="<?php echo $image;?>">Test</a>

EDIT: Actually I am still unclear of your usage when you do this:

<a href="<?php echo $image;?>">Test</a>

You are setting the image URL in an <a href></a>? Why? Should it not be like this:

<img src="<?php echo $image;?>" />

Upvotes: 1

Related Questions