Skip to content

Instantly share code, notes, and snippets.

@dvoracek-jakub
Last active May 1, 2024 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dvoracek-jakub/ce7ca24a15e4f77b5e67d827e9db6ed7 to your computer and use it in GitHub Desktop.
Save dvoracek-jakub/ce7ca24a15e4f77b5e67d827e9db6ed7 to your computer and use it in GitHub Desktop.
Wordpress navigation in Bootstrap style
// Usage in a template
\YourCompany\Helpers\Pagination::render(get_query_var('paged'), $loop->max_num_pages);
<?php
/**
* Class for rendering Wordpress navigation in Bootstrap style
*/
namespace YourCompany\Helpers;
Class Pagination
{
public static function render($paged, $max_num_pages, $echo = true)
{
$largerInt = 999999999; // need an unlikely integer
$pages = paginate_links([
'base' => str_replace($largerInt, '%#%', esc_url(get_pagenum_link($largerInt))),
'format' => '?paged=%#%',
'current' => max(1, $paged),
'total' => $max_num_pages,
'type' => 'array',
'prev_next' => true,
'prev_text' => __('« Prev'),
'next_text' => __('Next »'),
]);
if (is_array($pages)) {
$paged = ($paged == 0) ? 1 : $paged;
$pagination = '<ul class="pagination">';
foreach ($pages as $page) {
//$page = strip_tags($page);
$pagination .= '<li class="page-item">' . str_replace('page-numbers', 'page-link', $page) . '</li>';
}
$pagination .= '</ul>';
if ($echo) {
echo $pagination;
} else {
return $pagination;
}
}
}
}
@Moutaz-homsi
Copy link

Moutaz-homsi commented Jan 17, 2021

Hi , you can add this line to test if link is active .
$current_class = strpos($page, 'current') ? 'active' : '';
then adding this class to li .
Thanks for sharing your code with us

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment