admin管理员组

文章数量:1122832

I have implemented pagination to my wp site and pagination works correctly, but when I limit to display lets say 10 posts and I have 9 then it won't show pagination.

Is it possible to show in that case 1?

I read that paginate_links will return void if under 2, but is that somehow changeable? (string|array|void) String of page links or array of page links, depending on 'type' argument. Void if total number of pages is less than 2.

My code currently. It shows pages 1 and 2 when posts > 10, but won't show 1 if posts < 10:

    global $wp_query;
        $args_pagination = array(
                        'base' => add_query_arg('paged', '%#%'),
                        'total' => $wp_query->max_num_pages,
                        'current' => $paged,
                        'prev_next' => false);
        $html .= paginate_links($args_pagination);

Edit: This all function is used as shortcode to use custom pagination in random places of my site (Not random, but not in the place pagination usually is). I used query_posts for that as I honestly don't know that much about wp developement.

extract(shortcode_atts(array('limit' => 1000, 'type' => 'post'), $atts ));
$paged = get_query_var('paged') ? get_query_var('paged') : 1;  

query_posts(array( 
        'posts_per_page' => $limit, 
        'post_type' => $type, 
        'order' => 'DESC',
        'orderby' =>'date', 
        'paged' => $paged));

global $wp_query;
$args_pagination = array(
        'base' => add_query_arg('paged', '%#%'),
                    'total' => $wp_query->max_num_pages,
                    'current' => $paged,
                    'prev_next' => false);
    $html .= paginate_links($args_pagination);

And I returned html to display it on my pre-designed box.

I have implemented pagination to my wp site and pagination works correctly, but when I limit to display lets say 10 posts and I have 9 then it won't show pagination.

Is it possible to show in that case 1?

I read that paginate_links will return void if under 2, but is that somehow changeable? (string|array|void) String of page links or array of page links, depending on 'type' argument. Void if total number of pages is less than 2.

My code currently. It shows pages 1 and 2 when posts > 10, but won't show 1 if posts < 10:

    global $wp_query;
        $args_pagination = array(
                        'base' => add_query_arg('paged', '%#%'),
                        'total' => $wp_query->max_num_pages,
                        'current' => $paged,
                        'prev_next' => false);
        $html .= paginate_links($args_pagination);

Edit: This all function is used as shortcode to use custom pagination in random places of my site (Not random, but not in the place pagination usually is). I used query_posts for that as I honestly don't know that much about wp developement.

extract(shortcode_atts(array('limit' => 1000, 'type' => 'post'), $atts ));
$paged = get_query_var('paged') ? get_query_var('paged') : 1;  

query_posts(array( 
        'posts_per_page' => $limit, 
        'post_type' => $type, 
        'order' => 'DESC',
        'orderby' =>'date', 
        'paged' => $paged));

global $wp_query;
$args_pagination = array(
        'base' => add_query_arg('paged', '%#%'),
                    'total' => $wp_query->max_num_pages,
                    'current' => $paged,
                    'prev_next' => false);
    $html .= paginate_links($args_pagination);

And I returned html to display it on my pre-designed box.

Share Improve this question edited Jul 7, 2020 at 6:58 Kertix asked Jul 6, 2020 at 15:00 KertixKertix 34 bronze badges 9
  • What is the actual question? It seems that you have solved the initial question with your own code that shows 1 page if there are less than 11 posts. – jdm2112 Commented Jul 6, 2020 at 15:05
  • It won't show 1, it returns void and shows empty space – Kertix Commented Jul 6, 2020 at 15:06
  • "Currently I have it like that and it shows 1 and 2 if I have 11 posts:" what does this mean? – jdm2112 Commented Jul 6, 2020 at 15:06
  • Edited that sentence a bit – Kertix Commented Jul 6, 2020 at 15:08
  • 1 Why do you need to use the $wp_query global? Have you done the unthinkable and used query_posts and now need custom pagination to fix things? Why is this being returned as a string instead of output directly? In order to find a solution we're going to need to know the surrounding context that explains the need for the function and where the values come from for the function call and its arg. Use the edit link under the question tags to add as much information as you can, the key information might even be something you consider obvious or irrelevant, oversharing is good – Tom J Nowell Commented Jul 6, 2020 at 16:36
 |  Show 4 more comments

1 Answer 1

Reset to default 1

It's not possible. paginate_links() is hard-coded to stop producing output as soon as it notices that there'll be less than 2 pages. You can see this on lines 4130–4132 of the function's source code here: https://developer.wordpress.org/reference/functions/paginate_links/#source

The only workaround would be to copy the code of paginate_links() into your own new function and change the 2 to 1 on what is line 4130 for the existing function.

本文标签: paginate linksShow number of pagination pages even when total under two