Support » Plugins and Hacks » Filter and display attachments

  • Resolved mohawktopus

    (@mohawktopus)


    Hello all, this should be a quick fix (hah!), I just don’t have the php skills.

    This is in my sidebar.php displaying all media uploaded to my wordpress installation. I want to filter it so that it only will display media if it has certain file extensions, (i.e. pdf, doc, rtf). What do I need to change/add in order for that to happen?

    <?php
    
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => -1,
    	'post_status' => null,
    	'post_parent' => null, // any parent
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $post) { ?>
    <li>
    <?php
    		setup_postdata($post);
    		the_attachment_link($post->ID, true);
    ?>
    </li>
    
    <?php
    	}
    }
    
    ?>
Viewing 9 replies - 1 through 9 (of 9 total)
  • I found this snippet:

    <?php
    // list of approved file extensions
    $approved = array (
        'pdf' => "application/pdf",
        'doc' => "application/msword",
        'gif' => "image/gif",
        'jpg' => "image/jpeg",
        'jpeg' => "image/jpeg"
    };
    
    // for display purposes
    $filename = "index.html";
    
    // get the filename
    $ext = pathinfo($filename);
    
    switch (array_key_exists($ext['extension'], $approved)) {
        case true:
            break; // do nothing, break out...
        case false:
            echo $ext['extension'] ." is invalid!";
            exit;
        break;
    }
    
    ?>

    Is there anyway to integrate this with the above to solve my problem?

    I finally made a solution! I feel so proud 🙂

    Put this in sidebar.php:

    <li><h2>Recent Attachments</h2>
    <ul>
    <?php
    $valid_ext = array("pdf", "doc", "rtf", "zip");
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => -1,
    	'post_status' => null,
    	'post_parent' => null, // any parent
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $post) {
    $ext = getFileExt(wp_get_attachment_url($post->ID, false));
    if(in_array($ext, $valid_ext)) {
     ?>
    <li>
    <?php
    	setup_postdata($post);
    	the_attachment_link($post->ID, true);
    ?>
    </li>
    <?php
                    }
    	}
    }
    ?>

    And define getFileExt() in wp-includes/functions.php by pasting this at the bottom (before the final ?>):

    function getFileExt($file) {
         return strtolower(substr(strrchr($file,'.'),1));
    }

    This will put the latest uploads of particular extensions (defined by you in the $valid_ext array) in your sidebar. I needed this because I’m creating a website for a Boy Scout troop, and the latest permission slips and agendas need to be on the front page, while any images or other media they may upload do not.

    Hope this helps out someone else, too.

    thank you! this is almost exactly what i’m looking for! 😛

    i’m needing this list to generate the list of attachments of the current post

    can the above script be tailored to accomplish this?

    thanks for your help!
    regards

    @yellowllama: you might want to change
    'post_parent' => null, // any parent
    in
    'post_parent' => $post->ID,

    OK. That was a bit short. I’ve puzzled together the following, grabbing code from forum. This creates a bullet list of .pdf and .doc attachments.

    <ul>
        <?php
            $args = array(
              'post_type' => 'attachment',
              'post_mime_type' => 'application/pdf,application/msword',
              'numberposts' => -1,
              'post_status' => null,
              'post_parent' => $post->ID,
              'orderby' => 'menu_order',
              'order' => 'desc'
              );
            $attachments = get_posts($args);
            if ($attachments) {
              foreach ($attachments as $attachment) {
                echo '<li><a href="'.wp_get_attachment_url($attachment->ID).'">';
                echo $attachment->post_title;
                echo '</a></li>';
              }
            }
        ?>
        </ul>

    Works just great for me. Thanks 1

    Thanks 🙂

    I’ve got code similar to edde, only mine is with images.
    In this line:echo '<li><a href="'.wp_get_attachment_url($attachment->ID).'">';
    I’m having difficulty having the title of the image ($attachment->post_title) being dynamically inserted into the a href call:
    echo '<li><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox">';
    I’ve got the rel=”shadowbox” so that the text link will pop open an image shadowbox (this is working). In order to have a title on the pop up window I need to include this within the a href string…can any one offer a suggestion?
    Thanks!

    Figured out in case anyone will have the same problem in the future:

    <?php
            $args = array(
              'post_type' => 'attachment',
              'post_mime_type' => 'image',
              'numberposts' => -1,
              'post_status' => null,
              'post_parent' => $post->ID,
              'orderby' => 'menu_order',
              'order' => 'ASC'
              );
            $attachments = get_posts($args);
            if ($attachments) {
              foreach ($attachments as $attachment) {
                echo '<li><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
                echo $attachment->post_title;
                echo '</a></li>';
              }
            }
        ?>

    This will display the image caption in the shadow box.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Filter and display attachments’ is closed to new replies.