dlw760
6/20/2013 - 11:58 AM

Just a simple Shortcode to add downloads to any page (includes pagination which requires another shortcode). Add [downloads] to the page.

Just a simple Shortcode to add downloads to any page (includes pagination which requires another shortcode). Add [downloads] to the page.

<?php
function downloads($atts, $content = null) {

  extract(shortcode_atts(array(
		"pagination" => 'true',		
		"showposts" => '10',
		"category" => '',
		"type" => 'download'		
	), $atts));
	global $wp_query,$paged,$post;
	$temp = $wp_query;
	$wp_query= null;
	$wp_query = new WP_Query();
	if($pagination == 'true') $query .= '&paged='.$paged;
	if(!empty($category)) $query .= '&downloads_category='.$category;	
	if(!empty($showposts)) $query .= '&showposts='.$showposts;
	if(!empty($type)) $query .= '&post_type='.$type;	
	$wp_query->query($query);	
	ob_start();
	?>
	<?php $mycount =1;
	if (have_posts()) :  ?>      
      <?php   while ($wp_query->have_posts()) : $wp_query->the_post(); 
	  $even_odd = ( ' odd' != $even_odd ) ? ' odd' : ' even';  ?>    


		<article class="post<?php echo $even_odd;?><?php if (!has_post_thumbnail()) echo(' no_image');?>">	
			<?php if (has_post_thumbnail() ) : ?>
				<?php the_post_thumbnail('news'); ?>
	    	<?php endif; ?>	 			
	    	<div class="post-excerpt">				
				<h2 class="entry-title"><?php the_title(); ?></h2>					
				<p><?php the_excerpt();?></p> 
			</div>
			<div class="actions">
				<?php if (get_post_type()=='download') : 
	    			$files =  get_post_meta($post->ID, 'files', false); 
	    			foreach ($files as $file) : if ($file['guid']) : ?>
	    				<p class="file"><a href="<?php echo($file['guid']); ?>" target="_blank">Download File</a></p>
					<?php endif; endforeach;
				endif;?>
			</div>
		</article>	  	  
	<?php  $mycount++;endwhile; ?>

       <?php if(function_exists('dlw_pagination')) :
	   dlw_pagination(); 
	   else : ?>  
	<div class="navigation">
	  <div class="alignleft"><?php previous_posts_link('Previous') ?></div>
	  <div class="alignright"><?php next_posts_link('More') ?></div>
	</div>
	<?php endif; 
	else : ?>
	<p><strong>There are currently no downloads to display</strong></p>
	<?php endif;
	 $wp_query = null; $wp_query = $temp;
	$content = ob_get_contents();
	ob_end_clean();
	wp_reset_query();
	
	return $content;
}
add_shortcode("downloads", "downloads");