Skip to content

Instantly share code, notes, and snippets.

@phucdohong96
Last active October 7, 2017 05:49
Show Gist options
  • Select an option

  • Save phucdohong96/580cc5396cb94df8b1ac763771b1e183 to your computer and use it in GitHub Desktop.

Select an option

Save phucdohong96/580cc5396cb94df8b1ac763771b1e183 to your computer and use it in GitHub Desktop.
Custom Loop Pagination
<?php
/*----------------------------------ONLY WORK ON HOMEPAGE----------------------------------*/
function loop_custom_function( $atts ) {
ob_start();
$paged = isset( $_GET['paged'] ) ? (int) $_GET['paged'] : 1;
$opts = shortcode_atts( array(
'type' => '',
'page_link' => '',
), $atts );
$output = '';
$args = array(
'post_type' => $opts['type'],
'posts_per_page' => '6',
'order' => 'DESC',
);
$query = new WP_Query( $args );
$count = 0;
/*---------------------Loop-------------------*/
while ( $query->have_posts() ) : $query->the_post();
$id = get_the_ID();
$title = get_the_title();
$thumbnail_id = get_post_thumbnail_id( $id );
$image = wp_get_attachment_image( $thumbnail_id, array(495, 400) );
$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
$excerpt = get_the_excerpt();
$link = get_permalink ();
$content = get_the_content ();
?>
<div>HTML Content</div>
<?php
$count++;
endwhile;
echo '<div class="clearfix"></div>';
/*-------------------Pagination------------------*/
$pag_args = array(
'format' => ''.$opts["page_link"].'/?fwp_paged=%#%',
'current' => $paged,
'total' => $query->max_num_pages,
);
$pager = paginate_links( $pag_args );
echo '<div class="shortcode_pager">'.$pager.'</div>';
wp_reset_postdata();
return ob_get_clean();
}
<?php
include('cutom_loop_shortcode.php');
add_shortcode('loop_custom_shortcode', 'loop_custom_function');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment