Return to Snippet

Revision: 45977
at May 11, 2011 22:43 by bigevilbrain


Initial Code
/**
* CPT (Custom Post Type) Feed Shortcode
* http://codex.wordpress.org/Shortcode_API
*/
function fn_cpt( $atts = array(), $content = NULL ) {
	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();
	$defaults = array(
		'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 = '';
	
	ob_start();
	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
	
	$output = ob_get_clean();
	
	// Restore previous wp_query
	$wp_query = null; $wp_query = $saved_query;
	$post = null; $post = $saved_post;
	
	return $output;
}
add_shortcode('cptfeed', 'fn_cpt');

Initial URL

                                

Initial Description

                                

Initial Title
CPT (Custom Post Type) Feed Shortcode

Initial Tags
wordpress

Initial Language
PHP