sumonsbgc
4/18/2016 - 9:34 PM

Custom post loop with pagination in shortcode

Custom post loop with pagination in shortcode

<?php 

function portfolios_shortcode($atts){
	extract( shortcode_atts( array(
		'expand' => '',
	), $atts) );
    
    global $paged;
    $posts_per_page = 6;
    $settings = array(
        'showposts' => $posts_per_page, 
        'post_type' => 'portfolio', 
        'orderby' => 'menu_order', 
        'order' => 'ASC', 
        'paged' => $paged
    );
	
    $post_query = new WP_Query( $settings );	
    
    $total_found_posts = $post_query->found_posts;
    $total_page = ceil($total_found_posts / $posts_per_page);
		
	$list = '<div class="portfolio-item-list">';
	while($post_query->have_posts()) : $post_query->the_post();
		$list .= '
		<div class="single-portfolio-item">
        
		</div>
		';        
	endwhile;
	$list.= '</div>';
    
    if(function_exists('wp_pagenavi')) {
        $list .='<div class="page-navigation">'.wp_pagenavi(array('query' => $post_query, 'echo' => false)).'</div>';
    } else {
        $list.='
        <span class="next-posts-links">'.get_next_posts_link('Next page', $total_page).'</span>
        <span class="prev-posts-links">'.get_previous_posts_link('Previous page').'</span>
        ';
    }
    
	
	return $list;
}
add_shortcode('portfolios', 'portfolios_shortcode');