• I’m building a site to house info for several different college classes I teach. I’d like to be able to attach media (docs, audio, pdfs) to posts like normal (no problem there), but then have a central page for each individual class that contains all the media posted for a given class.

    I’m comfortable mucking around in templates and using Pages, but I have no idea how to get this done. Any advice?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter candlebain

    (@candlebain)

    I see how that works. Thanks for the link!

    Couple questions:

    Will this work for .doc files as well? I didn’t see it in the WP mime types.

    How would you go about separating the attachments by College Class? I plan to use a Category for each College Class (seems logical right now anyways), but I also want each Class on its own page.

    Thanks for the help.

    You should be able to test on $post->post_mime_type for each of the attachments:

    <?php $doctypes = array('pdf','msword','excel','powerpoint');
    foreach($doctypes as $doctype) {
    	if( stristr($post->post_mime_type, $doctype) ) :?>
    	[ do stuff ]
    	<?php endif;
    	break;
    }

    A category per Class seems to be a logical approach. That way, each class will have its own page by default and you can other work on the default category.php template file or you can create custom category templates for class-specific listings

    Thread Starter candlebain

    (@candlebain)

    Ok…after playing with this for awhile, I’m not getting anything working. Admittedly, this is a bit further into PHP than I thought I was going to get with this.

    I stumbled on get_posts(), which might work as well…here’s what I’m using:

    <?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) {
    		setup_postdata($post); ?>
    		<p><?php the_attachment_link($post->ID, false); ?><p>
    <?php	}
    }
    ?>

    First off…is this even viable? When I run it like it is, I get all my attachments, site-wide. It claims I can limit by category by using ‘category_name’, but when I do this, I get nothing. I’ve tried the slug and the actual category title, and several variations out of frustration.

    When I tried category_name, here’s how I did it:

    Everything was the same except for $args:

    $args = array(
    	'post_type' => 'attachment',
    	'category_name' => 'rock-history-night', // The slug for the category
    	'numberposts' => -1,
    	'post_status' => null,
    	'post_parent' => null // any parent
    	);

    Everything disappears…no results. What do you guys think?

    Thanks

    You need to use the actual name of the category for category_name – not the slug.

    $args = array(
    'post_type' => 'attachment',
    'category_name' => 'Rock History Night', // The title of the category
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => null // any parent
    );
    Thread Starter candlebain

    (@candlebain)

    Still not getting anything. To simplify, I changed the category name to Rock, and I’ve tried both upper and lowercase with no results.

    Are media files associated with the posts they are attached to as well as the groups assigned to the post? The only theory I have is that media files don’t inherit the category tags of their parent posts.

    For reference, I have two test posts in the Rock category that each have a pdf attachment. They only show when I remove the category sort, but then I get the whole site’s attachments.

    In case my code is faulty, here’s the code I’m using:

    <?php
    $args = array(
    'post_type' => 'attachment',
    'category_name' => 'Rock', // The title of the category
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => null
    );
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $post) {
    		setup_postdata($post);
    		the_title();
    		the_attachment_link($post->ID, false);
    		the_excerpt();
    	}
    }
    ?>

    Thanks for sticking with me on this. I appreciate the help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Themed Media (docs, pdf, audio…etc) Galleries’ is closed to new replies.