thachpham92

Untitled

Aug 28th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. <?php function list_posts_by_taxonomy( $post_type, $taxonomy, $get_terms_args = array(), $wp_query_args = array() ){
  2.     $tax_terms = get_terms( $taxonomy, $get_terms_args );
  3.     if( $tax_terms ){
  4.         foreach( $tax_terms  as $tax_term ){
  5.             $query_args = array(
  6.                 'post_type' => $post_type,
  7.                 "$taxonomy" => $tax_term->slug,
  8.                 'post_status' => 'publish',
  9.                 'posts_per_page' => 5,
  10.                 'ignore_sticky_posts' => true
  11.             );
  12.             $query_args = wp_parse_args( $wp_query_args, $query_args );
  13.  
  14.             $my_query = new WP_Query( $query_args );
  15.             if( $my_query->have_posts() ) { ?>
  16.                 <h2 id="<?php echo $tax_term->slug; ?>" class="tax_term-heading"><?php echo $tax_term->name; ?></h2>
  17.                 <ul>
  18.                 <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  19.                     <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
  20.                 <?php endwhile; ?>
  21.                 </ul>
  22.                 <?php
  23.             }
  24.             wp_reset_query();
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment