admin管理员组文章数量:1130349
I found this code :
// Get total number of pages
global $wp_query;
$total = $wp_query->max_num_pages;
// Only paginate if we have more than one page
if ( $total > 1 ) {
// Get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// Structure of “format” depends on whether we’re using pretty permalinks
$permalinks = get_option('permalink_structure');
$format = empty( $permalinks ) ? '&page=%#%' : 'page/%#%/';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 2,
'type' => 'list'
));
}
It works ok but it generates pagination like "1, 2, 3 ... LASTPAGENUMBER", I've seen plugins that generate paginations like this "1, 2, 3... 19, 20, 30 ... LASTPAGENUMBER".
Can this be achieved using paginate_links() function?
I don't want to add a plugin just for this small tweak.
I found this code :
// Get total number of pages
global $wp_query;
$total = $wp_query->max_num_pages;
// Only paginate if we have more than one page
if ( $total > 1 ) {
// Get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// Structure of “format” depends on whether we’re using pretty permalinks
$permalinks = get_option('permalink_structure');
$format = empty( $permalinks ) ? '&page=%#%' : 'page/%#%/';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 2,
'type' => 'list'
));
}
It works ok but it generates pagination like "1, 2, 3 ... LASTPAGENUMBER", I've seen plugins that generate paginations like this "1, 2, 3... 19, 20, 30 ... LASTPAGENUMBER".
Can this be achieved using paginate_links() function?
I don't want to add a plugin just for this small tweak.
Share Improve this question asked Nov 12, 2011 at 19:08 tryontryon 311 gold badge1 silver badge3 bronze badges4 Answers
Reset to default 4The paginate_links() function does this by default. The controlling parameter is mid_size, which determines the number of page links around the current page to display. The default value is 2.
What this means is that, assuming you have 12 pages, and the current page is Page 1, the pagination will look like:
1 2 3 ... 12
But if the current page is Page 6, the pagination will look like:
1 ... 4 5 6 7 8 ... 12
I could be mistaken, but I think that's how must pagination-link Plugins work.
Edit
Sorry, I misread your question at first, and didn't grasp that you want to output every tenth page in your pagination links.
This function doesn't have a built-in parameter to do what you want. Your best bet might be to set 'show_all' to true, change 'type' to 'array', and then construct the output yourself, by looping through the array values.
http://codex.wordpress/Function_Reference/paginate_links
What you are looking for is 'end_size', add this to argument array with the value you want.
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args= new WP_Query(array(
'post_type'=>'card',
'posts_per_page' => 6,
'paged' => $paged,
));
if($args->have_posts()) :
while($args->have_posts()) : $args->the_post();
$permalink=get_the_permalink(get_the_ID());
$title=get_the_title();
echo '<div class="col-md-4 card-details"><a href="'.$permalink.'">';
echo '<div class="card-img">';the_post_thumbnail('thumbnail');echo '</div>';
echo '<div class="card-title"><h4>'.$title.'</h4></a>';
echo'</div>';
echo '</div>';
endwhile;
$total_pages = $args->max_num_pages;
?>
<div class="greetings-pagination col-md-12">
<?php
if ($total_pages > 1){
$current_page = max(1, get_query_var('page'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('prev'),
'next_text' => __('next'),
));
}
?>
</div>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
In WordPress developer website it did mention some parameters.This is the page about this function.You can check 'end_size' for more information.
And I still got a question.I use <?php echo paginate_links(); ?> in my theme template and it works perfectly(As the picture shows).
But I am not sure if it would cause some other problems.
I found this code :
// Get total number of pages
global $wp_query;
$total = $wp_query->max_num_pages;
// Only paginate if we have more than one page
if ( $total > 1 ) {
// Get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// Structure of “format” depends on whether we’re using pretty permalinks
$permalinks = get_option('permalink_structure');
$format = empty( $permalinks ) ? '&page=%#%' : 'page/%#%/';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 2,
'type' => 'list'
));
}
It works ok but it generates pagination like "1, 2, 3 ... LASTPAGENUMBER", I've seen plugins that generate paginations like this "1, 2, 3... 19, 20, 30 ... LASTPAGENUMBER".
Can this be achieved using paginate_links() function?
I don't want to add a plugin just for this small tweak.
I found this code :
// Get total number of pages
global $wp_query;
$total = $wp_query->max_num_pages;
// Only paginate if we have more than one page
if ( $total > 1 ) {
// Get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// Structure of “format” depends on whether we’re using pretty permalinks
$permalinks = get_option('permalink_structure');
$format = empty( $permalinks ) ? '&page=%#%' : 'page/%#%/';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 2,
'type' => 'list'
));
}
It works ok but it generates pagination like "1, 2, 3 ... LASTPAGENUMBER", I've seen plugins that generate paginations like this "1, 2, 3... 19, 20, 30 ... LASTPAGENUMBER".
Can this be achieved using paginate_links() function?
I don't want to add a plugin just for this small tweak.
Share Improve this question asked Nov 12, 2011 at 19:08 tryontryon 311 gold badge1 silver badge3 bronze badges4 Answers
Reset to default 4The paginate_links() function does this by default. The controlling parameter is mid_size, which determines the number of page links around the current page to display. The default value is 2.
What this means is that, assuming you have 12 pages, and the current page is Page 1, the pagination will look like:
1 2 3 ... 12
But if the current page is Page 6, the pagination will look like:
1 ... 4 5 6 7 8 ... 12
I could be mistaken, but I think that's how must pagination-link Plugins work.
Edit
Sorry, I misread your question at first, and didn't grasp that you want to output every tenth page in your pagination links.
This function doesn't have a built-in parameter to do what you want. Your best bet might be to set 'show_all' to true, change 'type' to 'array', and then construct the output yourself, by looping through the array values.
http://codex.wordpress/Function_Reference/paginate_links
What you are looking for is 'end_size', add this to argument array with the value you want.
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args= new WP_Query(array(
'post_type'=>'card',
'posts_per_page' => 6,
'paged' => $paged,
));
if($args->have_posts()) :
while($args->have_posts()) : $args->the_post();
$permalink=get_the_permalink(get_the_ID());
$title=get_the_title();
echo '<div class="col-md-4 card-details"><a href="'.$permalink.'">';
echo '<div class="card-img">';the_post_thumbnail('thumbnail');echo '</div>';
echo '<div class="card-title"><h4>'.$title.'</h4></a>';
echo'</div>';
echo '</div>';
endwhile;
$total_pages = $args->max_num_pages;
?>
<div class="greetings-pagination col-md-12">
<?php
if ($total_pages > 1){
$current_page = max(1, get_query_var('page'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('prev'),
'next_text' => __('next'),
));
}
?>
</div>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
In WordPress developer website it did mention some parameters.This is the page about this function.You can check 'end_size' for more information.
And I still got a question.I use <?php echo paginate_links(); ?> in my theme template and it works perfectly(As the picture shows).
But I am not sure if it would cause some other problems.
本文标签: Using paginatelinks() to generate quot123102030
版权声明:本文标题:Using paginate_links() to generate "1, 2, 3 ... 10, 20, 30, 40... 55" paginations 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1745515027a2153999.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论