Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type named "Resources" which has a Taxonomy named "Types." I have it manually pulling each term which isn't the most elegant solution and every time there is a new term added, I have to add it in manually.

I need a way for the Terms to populate with the posts associated listed below. I have a custom loop for displaying each post (pulling in custom fields) so I need to have control over what is displayed for each post.

Is there an easy way to go about this? Thanks.

share|improve this question
    
Could you paste the full code of your template into pastebin.com and post the link to it here. –  Joshua Abenazer Nov 23 '11 at 4:33
    
Here's a link to the code I have pastebin.com/L4THvK1a . Essentially, it's posting each taxonomy under the custom post type Resources manually. From there, with each post, it's checking if its a PDF or Video. I basically want to reduce that to one loop which just lists all the Custom Taxonomies. Any ideas? –  nurain Nov 28 '11 at 21:09

2 Answers 2

up vote 3 down vote accepted

Try this piece of code for page template. I've used it on one of my projects. It outputs taxonomy term one by one with list of all posts with this term. (Just replace YOUR_TAXONOMY_SLUG to yours)

<div id="content">
<h2 class="entry-title"><?php the_title(); ?></h2>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    $mytaxonomy = get_terms('YOUR_TAXONOMY_SLUG', array("fields" => "names"));
    <?php for ( $myterm = 0; $myterm < count($mytaxonomy); $myterm++) { ?>
        <h3><?php echo $mytaxonomy[$myterm]; ?>:</h3>
        <ul class="taxonomy_group">
        <?php $loop = new WP_Query(array('YOUR_TAXONOMY_SLUG' => $mytaxonomy[$myterm]));
            while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
            <?php endwhile; ?>  
        </ul>
    <?php } ?>
    </div><!-- #post-## -->
</div><!-- #content -->
share|improve this answer
    
PS: you can even replace FOR loop to FOREACH... –  YoYurec Dec 8 '11 at 22:33
    
This looks like it should work. Thanks! –  nurain Jan 18 '12 at 18:04
    
Does anyone know how efficient this is performance wise? –  Manny Fleurmond Jan 24 '12 at 14:57

Take note, this loop will only work if the category is a single word. WP_Query requires the category SLUG in the loop. If the category name is two words or more separated by nonbreaking space, it will not work (I can verify that because I just tried it). I still haven't found a way to pull the slug from the given taxonomy term, but I'm working on it.

ADDENDUM: Think I found a way to get around the multiple-word term name problem. Seems to work in my code.

    <div id="content">
<h2 class="entry-title"><?php the_title(); ?></h2>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $terms = get_terms('YOUR_TAXONOMY_SLUG');
    $count = count($terms); ?>
<?php if ($count > 0) {
    foreach ( $terms as $term) { ?>
        <h3><?php echo $term->name; ?></h3>    
        <ul class="taxonomy_group">
        <?php $loop = new WP_Query(array('YOUR_TAXONOMY_SLUG' => $term->slug));
            while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
            <?php endwhile; ?>  
        </ul>
    <?php }
    } ?>
    </div><!-- #post-## -->
</div><!-- #content -->

Let me know if this works for anyone else or they see any holes in it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.