Last active
March 21, 2018 08:15
-
-
Save ozamancse/b7dbba3c843aabddb301a7dda43b4e75 to your computer and use it in GitHub Desktop.
Shortcode custom post loop with pagination in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment