Posted By

bigevilbrain on 05/11/11


Tagged

wordpress


Versions (?)

CPT (Custom Post Type) Feed Shortcode


 / Published in: PHP
 

  1. /**
  2. * CPT (Custom Post Type) Feed Shortcode
  3. * http://codex.wordpress.org/Shortcode_API
  4. */
  5. function fn_cpt( $atts = array(), $content = NULL ) {
  6. global $post, $wp_query;
  7.  
  8. $cpt = 'project'; // Custom Post Type slug
  9.  
  10. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  11.  
  12. // Save and overwrite wp_query so the navigation will work
  13. $saved_query = $wp_query; $wp_query = null;
  14. $saved_post = $post; $post = null;
  15.  
  16. // http://codex.wordpress.org/Function_Reference/WP_Query
  17. // http://codex.wordpress.org/Template_Tags/get_posts
  18. $wp_query = new WP_Query();
  19. $defaults = array(
  20. 'orderby' => 'menu_order', // or 'date'
  21. 'order' => 'ASC', // or 'DSC'
  22. 'post_type' => $cpt,
  23. // $term->taxonomy => $term->slug,
  24. 'post_status' => 'publish',
  25. // 'posts_per_page' => get_option('posts_per_page', 10),
  26. // 'paged' => $paged,
  27. 'posts_per_page' => -1
  28. );
  29. $wp_query->query($defaults); // posts_per_page
  30.  
  31. $output = '';
  32.  
  33. if (have_posts()) {
  34. $count = 0;
  35. while (have_posts()) {
  36. the_post();
  37. get_template_part('templates/'.$cpt, 'excerpt'); // templates/project-excerpt.php
  38. if ($count % 4 == 3) {
  39. echo('<div class="end"></div>');
  40. }
  41. $count++;
  42. }
  43. }
  44.  
  45. // Pagination
  46. get_template_part('templates/nav', 'page'); // templates/nav-page.php
  47.  
  48. $output = ob_get_clean();
  49.  
  50. // Restore previous wp_query
  51. $wp_query = null; $wp_query = $saved_query;
  52. $post = null; $post = $saved_post;
  53.  
  54. return $output;
  55. }
  56. add_shortcode('cptfeed', 'fn_cpt');

Report this snippet  

You need to login to post a comment.