Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Wordpress - check if loop contains posts with attachments?
  2. function gotImages()
  3. {
  4.     $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') );
  5.     return ( !empty($attachments) ? true : false );
  6. }
  7.        
  8. function gotImages()
  9. {
  10.     global $wp_query;
  11.     $posts = $wp_query->posts;
  12.  
  13.     if ( empty( $posts ) )
  14.     return $posts;
  15.  
  16.     $searchImages = '~<img [^>]* />~';
  17.  
  18.     foreach ($posts as $post) {
  19.         $content = $post->post_content;
  20.         preg_match_all( $searchImages, $content, $countImages );
  21.         $images += count($countImages[0]);
  22.     }
  23.     return ( $images >= 1 ? true : false );
  24. }
  25.        
  26. if( !is_admin() && gotImages() == true ) :
  27.     wp_register_script('fancybox', get_stylesheet_directory_uri() . '/libs/fancybox/jquery.fancybox.pack.js', array('jquery'), false, true);
  28.     wp_register_script('fancybox.init', get_stylesheet_directory_uri() . '/js/init/lightbox.init', array('fancybox'), false, true);
  29.  
  30.     wp_enqueue_script('fancybox');
  31.     wp_enqueue_script('fancybox.init');
  32. endif;