Created
June 22, 2022 00:02
-
-
Save Tmeister/1159ed322d8c74c0522aca9af9f8532d to your computer and use it in GitHub Desktop.
Paginate Links example
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 | |
/** | |
* 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