WordPress Portfolio Custom Post Type Filtering


/ Published in: PHP
Save to your folder(s)

Here is a quick example on how to generate your own portfolio with filtering based on a taxonomy (in this case 'Solutions').


Copy this code and paste it in your HTML
  1. <?php
  2. // get portfolio
  3. $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 100, 'orderby' => 'title', 'order' => 'asc' );
  4. $loop = new WP_Query( $args );
  5. $port = array();
  6. while ( $loop->have_posts() ) : $loop->the_post();
  7. $idx = get_the_ID();
  8. $year_completed = get_post_meta($idx, 'year_completed', true);
  9. $website_addr = get_post_meta($idx, 'website_address', true);
  10. $thumb = get_the_post_thumbnail($idx, 'thumbnail'); //250x200 - featured image
  11. $title = get_the_title();
  12. $excerpt = get_the_excerpt();
  13. $content = get_the_content();
  14.  
  15. //get 'Solutions' terms
  16. $terms = get_the_terms($idx, 'Solutions');
  17. $terms_string = '';
  18.  
  19. //build up comma delimited string
  20. foreach($terms as $t){
  21. $terms_string .= $t->slug . ' ';
  22. }
  23. $port[] = array(
  24. 'id' => $idx,
  25. 'year_completed' => $year_completed,
  26. 'website' => $website_addr,
  27. 'thumb' => $thumb,
  28. 'title' => $title,
  29. 'content' => $content,
  30. 'excerpt' => $excerpt,
  31. 'terms' => $terms,
  32. 'terms_string' =>$terms_string, //classifications (comma delimited slugs)
  33. 'permalink' => get_permalink(),
  34. );
  35. endwhile;
  36.  
  37. $terms = get_terms('Solutions');
  38.  
  39. $filters = '<section id="options" class="clearfix">
  40. <ul id="filters" class="option-set floated clearfix">
  41. <li><a href="#filter" data-filter="*" class="selected">show all</a></li>';
  42.  
  43. foreach($terms as $t){
  44. $filters .= '<li><a href="#filter" data-filter=".' . $t->slug . '">' . $t->name . '</a></li>';// $t->count
  45. }
  46. $filters .= '</ul></section>';
  47. ?>
  48.  
  49.  
  50. <!-- add this inside entry-content -->
  51. <?php echo $filters; ?>
  52. <div id="portfolio">
  53.  
  54. <!-- isotope -->
  55. <ul class="thumbnails isotope">
  56. <?php foreach($port as $p){ ?>
  57. <li class="span3 element <?php echo $p['terms_string']; ?>">
  58. <div class="thumbnail">
  59. <?php echo $p['thumb']; ?>
  60. <div class="caption">
  61. <h5><a href="<?php echo $p['permalink']; ?>"><?php echo $p['title']; ?></a></h5>
  62. </div><!-- end caption -->
  63. </div><!-- end thumbnail -->
  64. </li>
  65. <? } //end foreach ?>
  66. </ul>
  67.  
  68. </div><!-- end #portfolio -->

URL: http://toddwilson.icreativepro.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.