dragunoff

wpquestions.com, ID = 3573

Dec 19th, 2011
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <?php
  2.  
  3. // set vars
  4. $post_type = 'testimonials';
  5. $taxonomy = 'products';
  6.  
  7. // get all products (respective terms form "product" taxonomy)
  8. $products = get_terms( $taxonomy, array() );
  9.  
  10. if ( $products ) {
  11.  
  12.     // start buildind output
  13.     $r = '';
  14.    
  15.     foreach ( $products as $product ) {
  16.        
  17.         // get posts
  18.         query_posts( array(
  19.             'tax_query' => array(
  20.                 array(
  21.                     'taxonomy' => $taxonomy,
  22.                     'field' => 'id',
  23.                     'terms' => $product->term_id
  24.                 )
  25.             ),
  26.             'post_type' => $post_type,
  27.             'posts_per_page' => 5
  28.             )
  29.         );
  30.        
  31.         // if there are any posts
  32.         if ( have_posts() ) {
  33.            
  34.             // product name
  35.             $r .= '<h2>Latest Testimonials for ' . $product->name . '</h2>';
  36.        
  37.             // the loop
  38.             while( have_posts() ) { the_post();
  39.                
  40.                 $r .= apply_filters( 'the_content', get_the_content() );
  41.                
  42.             }
  43.            
  44.         // no posts found
  45.         } else {
  46.        
  47.             $r .= '<h2>No Testimonials for ' . $product->name . ' Product</h2>';
  48.        
  49.         }
  50.        
  51.         // reset query
  52.         wp_reset_query();
  53.    
  54.     }
  55.    
  56.     // print out the resulting markup
  57.     echo $r;
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment