admin管理员组文章数量:1122832
I want to render the WordPress posts pagination markup with my own HTML markup. My theme has a pagination markup with ul > li
format. But the WordPress default pagination markup is different than my markup.
How can I apply my own HTML markup instead of the default pagination markup?
I want to render the WordPress posts pagination markup with my own HTML markup. My theme has a pagination markup with ul > li
format. But the WordPress default pagination markup is different than my markup.
How can I apply my own HTML markup instead of the default pagination markup?
Share Improve this question asked Aug 14, 2018 at 9:05 Eh JewelEh Jewel 8553 gold badges14 silver badges29 bronze badges 1- 1 Depends on what you use to print the pagination. Show us the code. – Max Yudin Commented Aug 14, 2018 at 9:22
2 Answers
Reset to default 0do you mean post archive pagination? The paginate_links()
function accepts a type
parameter, so if all you need is a ul > li
format you could try out something like:
// archive pagination
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( [
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
`type` => 'list',
] );
If you need a completely custom markup, you could try setting type
param to 'array'
and then build whatever you need with a foreach
loop.
docs: https://developer.wordpress.org/reference/functions/paginate_links/
To customize the pagination markup in WordPress to match your theme’s desired structure (e.g., using <ul><li></li></ul>
), you can override the default pagination function provided by WordPress. Here’s how you can achieve this:
Use the paginate_links function with a custom callback WordPress's paginate_links() function allows you to customize the HTML structure of pagination. You can specify the format, and wrap the pagination links in your desired markup.
Create a Custom Pagination Function You can create a custom pagination function in your theme’s functions.php file that outputs pagination with your own markup:
function custom_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
$pagination = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'type' => 'array', // returns an array instead of a string
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
) );
if ( is_array( $pagination ) ) {
echo '<ul class="custom-pagination">';
foreach ( $pagination as $page ) {
echo '<li>' . $page . '</li>';
}
echo '</ul>';
}
}
- Implement the Custom Pagination in Your Theme Once the function is defined, you need to call it in your theme’s template files where pagination is needed. This is typically in index.php, archive.php, category.php, or search.php.
Example:
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Your loop code here
endwhile;
// Call the custom pagination function
custom_pagination();
else :
// No posts found
endif;
本文标签: navigationOverride the WordPress default pagination markup
版权声明:本文标题:navigation - Override the WordPress default pagination markup 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282209a1926570.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论