Advertisement
Guest User

taxonomy crawling for term posts

a guest
May 10th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.62 KB | None | 0 0
  1. <h1>Navigating the products</h1>  
  2.     <?php  
  3.         //Get a navigation including all products
  4.     ?>
  5.     <ul>
  6.         <?php
  7.             //http://codex.wordpress.org/Template_Tags/wp_list_categories
  8.             $args = array(              
  9.                 'show_option_all'    => null,
  10.                 'orderby'            => 'name',
  11.                 'order'              => 'ASC',
  12.                 'style'              => 'list',
  13.                 'show_count'         => 0,
  14.                 'hide_empty'         => 1,
  15.                 'use_desc_for_title' => 1,
  16.                 'child_of'           => 0,
  17.                 'feed'               => null,
  18.                 'feed_type'          => null,
  19.                 'feed_image'         => null,
  20.                 'exclude'            => null,
  21.                 'exclude_tree'       => null,
  22.                 'include'            => null,
  23.                 'hierarchical'       => true,
  24.                 'title_li'           => __( 'Product categories' ),
  25.                 'show_option_none'   => __('No product categories available'),
  26.                 'number'             => null,
  27.                 'echo'               => 1,
  28.                 'depth'              => 2,
  29.                 'current_category'   => 0,
  30.                 'pad_counts'         => 0,
  31.                 'taxonomy'           => 'product_category',
  32.                 //'walker'             => 'Walker_Category'
  33.                 'walker'             => new walker_taxonomy_term_posts()  
  34.             );
  35.            
  36.             class walker_taxonomy_term_posts extends Walker_Category {
  37.            
  38.                 function start_el(&$output, $category, $depth, $args) {
  39.            
  40.                     $this->category = $category;
  41.            
  42.                     parent::start_el($output, $category, $depth, $args);
  43.                 }
  44.            
  45.                 function end_el(&$output, $page, $depth, $args) {
  46.                     if ( 'list' != $args['style'] )
  47.                         return;
  48.                        
  49.                     //echo $this->category->term_id . ',';
  50.                     echo '<pre>';
  51.                     print_r($this);
  52.                     echo '</pre>';
  53.                        
  54.                     $posts = new WP_Query(
  55.                         array(                        
  56.                             'post_type' => 'products',
  57.                             'tax_query' => array(
  58.                                 array(
  59.                                     'taxonomy' => 'product_category',
  60.                                     'field' => 'id',
  61.                                     'terms' => $this->category->term_id
  62.                                 )
  63.                             ),
  64.                             'numberposts' => 3
  65.                         )
  66.                     );
  67.            
  68.                     if( !empty( $posts ) ) {
  69.            
  70.                         $posts_list = '<ul>';
  71.            
  72.                             while ( $posts->have_posts() ) : $posts->the_post();
  73.                                 $posts_list .= '<li><a href="' . get_permalink( $post->ID ) . '">'.get_the_title( $post->ID ).'</a></li>';
  74.                             endwhile;        
  75.                            
  76.                             wp_reset_query();
  77.                        
  78.                         $posts_list .= '</ul>';
  79.                     }else{
  80.                         $posts_list = '';
  81.                     }
  82.            
  83.                     $output .= "{$posts_list}</li>\n";
  84.                 }
  85.             }
  86.            
  87.             wp_list_categories($args);
  88.            
  89.             wp_reset_query();
  90.         ?>
  91.     </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement