0

I am working on a plugin which requires a custom taxonomy and 3 terms (Personal, Group & Pets). I already have the taxonomy set up with terms and some sample posts. I've created a template for my custom taxonomy which currently shows the same output for each category(term). I need the output to display only the posts within the current selected term. So if viewing from example.com/user_image_category/group/ - it should display only the posts for "group".

Please note I'm trying to do this without touching the theme folder. And, I apologize if the code is a bit... gross, to say the least. I've pieced together a few things I've found but have fixed quite a bit - I usually deal with themes and SMALL plugins.

plugin function:

add_filter('template_include', 'sui_user_image_cat_template', 99);
             custom taxonomy / category
function sui_user_image_cat_template( $template ) {
    if ( is_tax('sui_image_category') ) {
        $new_template = plugin_dir_path(__FILE__) . 'templates/taxonomy-user_image_category.php';
        if ( '' != $new_template ) {
            return $new_template;
        }
    }
    return $template;
}

taxonomy-user_image_category.php:

<?php
/**
 * user_images_category taxonomy archive
 */

get_header(); 

?>

<div id="container">
  <div id="content" role="main">

<h2>Contest Photos: </h2><br />


<div id="category-navigation">
  <?php
    // put these in UL LI format if time allows

    $terms = get_terms('sui_image_category');
    foreach ($terms as $term) {
      $term_link = get_term_link($term, 'user_images');
      if( is_wp_error($term_link) )
        continue;
      echo '<span class="category-link"><a href="' . $term_link . '">' . $term->name . '</a></span> ';
    }
  ?>
<br /><br />
</div>



<div id="gridContainer">
<?php
$c = 1; //init counter
$bpr = 3; //boxes per row


  $args = array(
    'post_type' => 'user_images',
    'post_status' => 'publish'    
  );

  $posts = new WP_Query($args);  

  if($posts->post_count){

      foreach($posts->posts as $user_image){
        $user_info = get_userdata($user_image->post_author);    
        $user_image_cats = get_the_terms($user_image->ID, 'sui_image_category');

        foreach($user_image_cats as $cat){
          $user_image_cat = $cat->name;
        }

        $post_thumbnail_id = get_post_thumbnail_id($user_image->ID);

  ?>
      <div class="contest-entry" id="post-<?php echo $post_thumbnail_id ?>">
        <div class="contest-image">
          <?php echo wp_get_attachment_link($post_thumbnail_id, 'medium'); ?>
        </div>
        <div class="row">
          <div class="votes half left-align">&hearts;</div>
          <div class="category half right-align">
            <a href="<?php echo get_term_link($cat) ?>">
              <?php echo $user_image_cat ?>
            </a>
          </div>
        </div>
        <p class="image-caption"><?php echo $user_image->post_title ?></p>
        <a class="author" href="#"><?php echo $user_info->user_login ?></a>
      </div>

      <?php if($c == $bpr) : ?>

        <div class="clr"></div>

      <?php $c = 0;
        endif;
      ?>
    <?php
      $c++;
    }  
  };

?>
<div class="clr"></div>
</div>

  </div><!-- #content -->

</div><!-- #container -->

<?php get_footer(); ?>
0

In the taxonomy-user_image_category.php the get_queried_object method will return the term object, which you can incorporate to your WP_Query. From top of my head, in your case it may look like this:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

$args = array(
  'post_type' => 'user_images',
  'post_status' => 'publish',
  'tax_query' => array(
      array(
          'taxonomy' => 'sui_image_category',
          'field' => 'id',
          'terms' => $term_id
      )
   )
);

$posts = new WP_Query($args);
  • Wow, thank you. I didn't even think it would be that easy. – gstricklind Oct 16 '13 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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