/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * CPT (Custom Post Type) Feed Shortcode * http://codex.wordpress.org/Shortcode_API */ global $post, $wp_query; $cpt = 'project'; // Custom Post Type slug $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Save and overwrite wp_query so the navigation will work $saved_query = $wp_query; $wp_query = null; $saved_post = $post; $post = null; // http://codex.wordpress.org/Function_Reference/WP_Query // http://codex.wordpress.org/Template_Tags/get_posts $wp_query = new WP_Query(); 'orderby' => 'menu_order', // or 'date' 'order' => 'ASC', // or 'DSC' 'post_type' => $cpt, // $term->taxonomy => $term->slug, 'post_status' => 'publish', // 'posts_per_page' => get_option('posts_per_page', 10), // 'paged' => $paged, 'posts_per_page' => -1 ); $wp_query->query($defaults); // posts_per_page $output = ''; if (have_posts()) { $count = 0; while (have_posts()) { the_post(); get_template_part('templates/'.$cpt, 'excerpt'); // templates/project-excerpt.php if ($count % 4 == 3) { echo('<div class="end"></div>'); } $count++; } } // Pagination get_template_part('templates/nav', 'page'); // templates/nav-page.php // Restore previous wp_query $wp_query = null; $wp_query = $saved_query; $post = null; $post = $saved_post; return $output; } add_shortcode('cptfeed', 'fn_cpt');