Skip to content

Instantly share code, notes, and snippets.

@Tmeister
Created June 22, 2022 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tmeister/1159ed322d8c74c0522aca9af9f8532d to your computer and use it in GitHub Desktop.
Save Tmeister/1159ed322d8c74c0522aca9af9f8532d to your computer and use it in GitHub Desktop.
Paginate Links example
<?php
/**
* Plugin Name: A simple hook
* Plugin URI: https://example.com/
* Description: Handle the basics with this plugin.
* Version: 1.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: John Doe
* Author URI: https://author.example.com/
* License: GPL v2 or later
*/
// Simple llamado en el footer
add_action('wp_footer', function () {
global $wp_query;
// Solo se ejecuta si hay más de una página.
if ($wp_query->max_num_pages > 1) {
$big_number = 999999999;
// Obten los links con paginate_links
$links = paginate_links([
'base' => str_replace($big_number, '%#%', esc_url(get_pagenum_link($big_number))),
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages,
'format' => '?paged=%#%',
'type' => 'array',
]);
// Recorre los links y usa tu lógica
if (is_array($links)) { ?>
<nav class="my-pagination" style="padding: 20px;">
<!--
Por cada link agregamos una clase custom llamada ""my-custom-class
aqui puedes parsear y reemplazar lo que quieras en el link
puedes usar regex o lo necesario para tu requerimiento.
-->
<?php foreach ($links as $link) : $link = str_replace('class="', 'class="my-custom-class ', $link) ?>
<?php echo $link; ?>
<?php endforeach; ?>
</nav>
<?php
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment