Gauge Interactive Blog Read to understand
Find out what is on our minds and what corners of the industry we explore. Read about the latest news at Gauge Interactive and feel free to comment on our posts.
Find out what is on our minds and what corners of the industry we explore. Read about the latest news at Gauge Interactive and feel free to comment on our posts.
When using Wordpress as a CMS you will invariably be needing to call upon your posts’ file attachments for use in the website. Out of the box Wordpress does not offer too much in the way of customizing and calling upon these files, other than the new gallery implementation, which is still very limited. With a little PHP magic we can call upon, sort and display all types of multimedia in various ways on your website. These methods can prove invaluable when trying to get things done and content up on your pages. The following is an example of a Blog Post with several different types of files attached to it for the purpose of showing how to call and display them with a custom icon based on the MIME Type of each file. This might be useful for, say, a news section on a website. You can create the post and add a title and short description (if desired), and upload the file(s). Then the code does the rest. This avoids the cumbersome “insert into post” action that is built into the blogging system. You can leave the post alone and just let the script retrieve the file for you. The whole idea here is to minimize the amount of work the administrator has to do for adding content to a website. This can be great for news archives, magazine websites with many pictures, school and university sites with online assignments, or any sort of forms that need to be downloaded.

To see a simple example of this in use, check out the demo page. http://www.gaugeinteractive.com/mime-type-example
Below is a sample from the code used to detect and display the icons and links. Enjoy!
<?php query_posts('p=1068');?> //Wordpress Query, just grabbing one post
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> //initialize Wordpress loop
<?php the_title();?>
<?php the_content();?>
<?php if ( $files = get_children(array( //do only if there are attachments of these qualifications
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'application/pdf', //MIME Type condition
))) : ?>
<?php foreach( $files as $file ) : ?> //setup array for more than one file attachment
<?php $file_link = wp_get_attachment_url($file->ID); ?> //get the url for linkage
<?php $file_name_array=explode("/",$file_link); ?> <?php $file_name=array_reverse($file_name_array); ?> //creates an array out of the url and grabs the filename
<div>
<a href="<?php echo $file_link;?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/mime/pdf.jpg" alt="PDF" /></a>
<a href="<?php echo $file_link;?>"><?php echo $file_name[0];?></a>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php if ( $files = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'application/msword',
))) : ?>
<?php foreach( $files as $file ) : ?>
<?php $file_link = wp_get_attachment_url($file->ID); ?>
<?php $file_name_array=explode("/",$file_link); ?> <?php $file_name=array_reverse($file_name_array); ?>
<div>
<a href="<?php echo $file_link;?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/mime/doc.jpg" alt="Word Document" /></a>
<a href="<?php echo $file_link;?>"><?php echo $file_name[0];?></a>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php if ( $files = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'application/zip',
))) : ?>
<?php foreach( $files as $file ) : ?>
<?php $file_link = wp_get_attachment_url($file->ID); ?>
<?php $file_name_array=explode("/",$file_link); ?> <?php $file_name=array_reverse($file_name_array); ?>
<div>
<a href="<?php echo $file_link;?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/mime/zip.jpg" alt="Zip File" /></a>
<a href="<?php echo $file_link;?>"><?php echo $file_name[0];?></a>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php if ( $files = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'audio/mpeg',
))) : ?>
<?php foreach( $files as $file ) : ?>
<?php $file_link = wp_get_attachment_url($file->ID); ?>
<?php $file_name_array=explode("/",$file_link); ?> <?php $file_name=array_reverse($file_name_array); ?>
<div>
<a href="<?php echo $file_link;?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/mime/mp3.jpg" alt="MP3 File" /></a>
<a href="<?php echo $file_link;?>"><?php echo $file_name[0];?></a>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; endif;?>
The theme is interesting, I will take part in discussion.
2. questions:
1. Where do you get the icons?
2. is this basically editing the loop? can’t/shouldn’t this be rolled into a plugin?