Support » Plugin: Attachments » Filter for just one type of attachment

  • Resolved orionrush

    (@orionrush)


    Im having trouble migrating to the new version because Im now not sure how to filter the attachments for a specific file type/mime. Below is a working example from before the rewrite which filters for images. Can someone help and point me in the right direction?

    Many thanks

    // ========================================
    // ! Returns attached images as a gallery
    // ========================================
    function tr_get_attachments_image($post){
     	global $post;
     	$content_attach = '';
     	$is_test = "";
    	if( function_exists( 'attachments_get_attachments' ) ){
    		$attachments = attachments_get_attachments();
    		$attachments_count = count( $attachments );
    
    		//see if there are any images
    		if ( $attachments_count ){
    			for ($i=0; $i < $attachments_count; $i++) {
            	$mime = strtok($attachments[$i]['mime'], "/");
    
              if ( strstr($mime, 'image') ) {
                $is_test = true;
              break;
              }
            }
            // if so only ouput the images
            if ( $is_test == true ) {
    			$content_attach = '[fancybox]<div class="attached_gallery">';
    			$content_attach .=  '<div class="attachment-title">';
    			$content_attach .= '<h4 class="gallery">Image Gallery</h4>';
    			$content_attach .= '</div>';
    			$content_attach .= '<ul id="" class="thumbnails gallery">';
    			for( $i=0; $i<$attachments_count; $i++ ){
    				// if attachment is jpeg, png, gif images extension
    				if($attachments[$i]['mime'] == 'image/jpeg' || $attachments[$i]['mime'] == 'image/png' || $attachments[$i]['mime'] == 'image/gif' ){
    					// get attachment data for full version and thumbnail version.
    					$full_image = wp_get_attachment_image_src($attachments[$i]['id'], 'full');
    					$thumb_image = wp_get_attachment_image_src($attachments[$i]['id'], 'thumbnail');
    
    					$content_attach .= '<li class="gallery-item brick" >';
    					$content_attach.= '<a class="fancybox thumbnail" rel="gallery" title="'.$attachments[$i]['title'].'" href="'.$full_image[0].'" >';
    					$content_attach .= '<img class="attachment-thumbnail" alt="' . $attachments[$i]['title'] . '" title="' . $attachments[$i]['title'] . '" src="'.$thumb_image[0].'" width="'.$thumb_image[1].'" height="'.$thumb_image[2].'"/>';
    					$content_attach .= '</a>';
    					$content_attach .= '</li>';
    					}
    			}
    			$content_attach .= '</ul>';
    			$content_attach .= '</div>';
    
    		}
    		echo apply_filters('the_content', $content_attach );
    		}
    		//if ($is_test != true ) echo "<p>no gallery?</p>";
    	}
    }

    http://wordpress.org/extend/plugins/attachments/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jonathan Christopher

    (@jchristopher)

    You can accomplish this with the new one, check out these two utility functions available:

    <?php $attachments = new Attachments( 'attachments' ); /* pass the instance name */ ?>
    <?php if( $attachments->exist() ) : ?>
      <h3>Attachments</h3>
      <ul>
        <?php while( $attachments->get() ) : ?>
          <li>
            Type: <?php echo $attachments->type(); ?><br />
            Subtype: <?php echo $attachments->subtype(); ?><br />
          </li>
        <?php endwhile; ?>
      </ul>
    <?php endif; ?>

    The type() is the primary segment of the mimetype (e.g. ‘image’) and the subtype() is the secondary segment of the mimetype (e.g. ‘png’)

    Thanks for coming back to me Jonathan,
    Im not working with it in a template but in a plugin.

    What I have so far is the following:

    function tr_get_attachments_image(){
    	$id = get_the_ID();
    	$is_test = false;
     	$content_attach = '';
     	$attachments = new Attachments( 'attachments', $id );
    	if( $attachments->exist() ){
    		while ( $attachments->get() ){
    			if($attachments->type()=="image"){
    				$is_test = true;
    				break;
    			}
    		}
    		if($is_test == true){
    			$content_attach = '[fancybox]<div class="attached_gallery">';
    			$content_attach .= '<div class="attachment-title"><h4 class="gallery">Image Gallery</h4></div>';
    			$content_attach .= '<ul id="" class="thumbnails gallery">';
    			while ( $attachments->get() ){
    				$mytype=get_post_mime_type($attachments->id('full'));
    				if($mytype=="image/jpeg" || $mytype=="image/gif"){
    					$full_image = wp_get_attachment_image_src($attachments->id('full'));
    					$thumb_image = wp_get_attachment_image_src($attachments->id('thumbnail'));
    					$content_attach .= '<li class="gallery-item brick" >';
    					$content_attach .= '<a class="fancybox thumbnail" rel="gallery" title="'. $attachments->field( 'title' ) .'" href="'. $full_image[0] .'" >';
    					$content_attach .= '<img class="attachment-thumbnail" alt="' . $attachments->field( 'title' ) . '" title="' . $attachments->field( 'title' ) . '" src="'. $thumb_image[0] .'" width="'. $thumb_image[1] .'" height="'.$thumb_image[2].'"/>';
    					$content_attach .= '</a>';
    					$content_attach .= '</li>';
    				}
    			}
    		}
    		$content_attach .= '</ul>';
    		$content_attach .= '</div> <!- end image gallery -->';
    		echo apply_filters('the_content', $content_attach );
    	}
    }

    Two issues, its not returning the correct number of images (its missing the first one) – we suspect its to do with my second while ( $attachments->get() )

    also Im not getting the correct urls for
    $full_image = wp_get_attachment_image_src($attachments->id('full'));
    $thumb_image = wp_get_attachment_image_src($attachments->id('thumbnail'));

    This seems to resolve the above issues – incase anyone has need of it.
    Happy 2013

    function tr_get_attachments_image(){
    	$id = get_the_ID();
    	$bFoundImages = false;
     	$content_attach_li = '';
     	$content_attach = '';
     	$attachments = new Attachments( 'attachments', $id );
    	if( $attachments->exist() ){
    		while ( $attachments->get() ){
    			if($attachments->type()=="image"){
      			    if($attachments->subtype()=="jpeg" || $attachments->subtype()=="gif" || $attachments->subtype()=="png"){
    					$bFoundImages = true;
    					$full_image = wp_get_attachment_image_src($attachments->id(),'full');
    					$thumb_image = wp_get_attachment_image_src($attachments->id(),'thumbnail');
    					$content_attach_li .= '<li class="gallery-item brick" >';
    					$content_attach_li .= '<a class="fancybox thumbnail" rel="gallery" title="'. $attachments->field( 'title' ) .'" href="'. $full_image[0] .'" >';
    					$content_attach_li .= '<img class="attachment-thumbnail" alt="' . $attachments->field( 'title' ) . '" title="' . $attachments->field( 'title' ) . '" src="'. $thumb_image[0] .'" width="'. $thumb_image[1] .'" height="'.$thumb_image[2].'"/>';
    					$content_attach_li .= '</a>';
    					$content_attach_li .= '</li>';
                    		}
    			}
    		}
    	}
    	if($bFoundImages){
    		$content_attach = '[fancybox]<div class="attached_gallery">';
    		$content_attach .= '<div class="attachment-title"><h4 class="gallery">Image Gallery</h4></div>';
    		$content_attach .= '<ul id="" class="thumbnails gallery">';
    		$content_attach .=	$content_attach_li;
    		$content_attach .= '</ul>';
    		$content_attach .= '</div>';
    		echo apply_filters('the_content', $content_attach );
    	}
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter for just one type of attachment’ is closed to new replies.