Last active
September 7, 2022 16:23
-
-
Save raselahmed7/b8ff6fb24d782c3b2c63 to your computer and use it in GitHub Desktop.
Custom post loop with pagination in shortcode
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'); |
Thank you. It's exactly what I've been looking for for a long time.
Most Welcome 🙂
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pagination should be