mercredi 17 août 2016

Wordpress: get post_thumbnail image else first attached image else no image

I have the following if-statement that gets the post_thumbnail, if doesn't exist then I display the first attached image.

I would like to add an extra else to display post with no image. How can I make the attached image to run only if exist/true? Any suggestions?

 <?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
 <div class="container"> 

    <?php
        $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),  array( 235,91 ), false, ''  );
     ?>

     <a style="float:left;" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
     <div style="float:right;background: url(<?php echo $src[0]; ?> );"></div>
     </a>

 <?php  } else {  ?>

 <?php
        $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image&order=desc');
            if($files) :
                $keys = array_reverse(array_keys($files));
                $j=0;
                $num = $keys[$j];
                $image=wp_get_attachment_image($num, 'thumbnail', true);
                $imagepieces = explode('"', $image);
                $imagepath = $imagepieces[1];
                $main=wp_get_attachment_thumb_url($num);
            endif;
 ?>

  <a style="float:left;" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
     <div style="float:right;background: url(<?php echo $main; ?> );"></div>
  </a>

 <?php  }   ?>

Aucun commentaire:

Enregistrer un commentaire