How to pagination custom array in wordpress

How to pagination custom array in wordpress

In this article I will show you: How to pagination custom array in wordpress using paginate_links function

	// Array here.
	$data_print = array();
	//**************************
	//
	// Pagination
	//
	//**************************
	$page = ! empty( $_GET['cpage'] ) ? (int) $_GET['cpage'] : 1;
	$total = $data_count; //total items in array    
	$limit = 10; //per page    
	$totalPages = ceil( $total/ $limit ); //calculate total pages
	$page = max($page, 1); //get 1 page when $_GET['cpage'] <= 0
	$page = min($page, $totalPages); //get last page when $_GET&#91;'cpage'&#93; > $totalPages
	$offset = ($page - 1) * $limit;
	if( $offset < 0 ) $offset = 0;
	//**************************
	//
	// Pagination
	//
	//**************************
    // offset array
    $data_print = array_slice( $data_print, $offset, $limit );
    for($i = 0; $i < count($data_print); $i++){
    	// show data here
    }
    // Show pagination here
    if($totalPages > 1){
		$arr_params = array ('cpage' => '%#%');
		$customPagHTML     =  '<div class="cs_pagination"><span>Page '.$page.' of '.$totalPages.' | </span>'.paginate_links( array(
		'base' => add_query_arg( $arr_params ),
		'format' => '',
		'prev_text' => __('&laquo;'),
		'next_text' => __('&raquo;'),
		'total' => $totalPages,
		'current' => $page
		)).'</div>';
		echo $customPagHTML;
	}

Comments

comments

Share