Forums

[resolved] Filter and display attachments (8 posts)

  1. mohawktopus
    Member
    Posted 10 months ago #

    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
    	}
    }
    
    ?>
  2. mohawktopus
    Member
    Posted 10 months ago #

    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?

  3. mohawktopus
    Member
    Posted 10 months ago #

    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.

  4. YellowLlama
    Member
    Posted 9 months ago #

    thank you! this is almost exactly what i'm looking for! :P

    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

  5. edde
    Member
    Posted 4 months ago #

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

  6. edde
    Member
    Posted 4 months ago #

    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>
  7. Tbuyle
    Member
    Posted 4 days ago #

    Works just great for me. Thanks 1

  8. anabelle
    Member
    Posted 3 days ago #

    Thanks :)

Reply

You must log in to post.

About this Topic