I rewrote the code galleries and stopped at the pagination. How can I get the maximum number of pages? at the moment I can not define it, and put a test for the number 5.

 (Here "for ($ i = 1; $ i <= 5; $ i ++)" here "if ($ paged <5))."

And how to make that work offset parameter in pagination? Now when I put the parameter offset, pagination will not work correctly. I need to post 16 pictures post_per_page was equal to 4. I have not found a solution for my specific case. Maybe it can be replaced with something? Here is my complete code in funсtions.php


function my_gallery($atts){
    $img_id = explode(',', $atts['ids']);
    if(!$img_id[0]) {
        return '<div class="no-img-gallery">No images</div>';

    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $pictures = get_posts( array(
        'posts_per_page' => 4,
        'post__in'       => $img_id,
        'paged' => $paged,
        'post_type'      => 'attachment',
    ) );

    $html = '<div class="gallery-my">';
    foreach ($pictures as $item){

        $img_desc = $item->post_content;
        $img_caption = $item->post_excerpt;
        $img_title = $item->post_title;
        $img_thumb = wp_get_attachment_image_src($item->ID);
        $img_full = wp_get_attachment_image_src($item->ID,medium);

        $html .="
        <div class='item-gallery'>
            <a href='{$img_full[0]}'>
                <img src='{$img_thumb[0]}' width='{$img_thumb[1]}' height='{$img_thumb[2]}' alt='{$img_title}'>
            <div class='desc-wrap'>
                    <strong class='title-img'>{$img_title}</strong>
                    <span class='desc-img'>{$img_desc}</span>


    $html .= "</div>";
    $html .= "<div class='pagination-gallery'>";
        if ($paged > 1) { 
            $html .="<span class='prev-cust'>
                        <a href='?paged=".($paged-1)."'><</a>
            $html .="<span class='num-pag'>
                <a href='?paged=".$i."'";


            $html .=$usl.">".$i."</a></span>";
        if ($paged < 5) { 
            $html .="<span class='prev-cust'>
                        <a href='?paged=".($paged+1)."'>></a>

    $html .= "</div>";                              

    return $html;


If you want max_num_pages, you need to use WP_Query instead of get_posts. You can then use $your_query_object->max_num_pages in your code.

offset and paged don't work together, because both of them ultimately set LIMIT in the MySQL query. offset query parameter will always override paged. If you want to offset the results, you need to do the math yourself, multiply page number by posts per page and add offset, and then only use offset. See Making Custom Queries using Offset and Pagination for more info.

  • Thanks for the answer. In the first part, I understood, he was replaced at the request WP_Query. Now everything works. But the second part can not understand. I created a function, as it is written in the manual, but the code does not react to it. Can you look at the code and tell me what is correct? Both functions are functions.php – harley Dec 7 '15 at 6:50
  • Sorry, big code makes to write comments – harley Dec 7 '15 at 6:53
  • To offset I copied and pasted the code in functions.php add_action('pre_get_posts', 'myprefix_query_offset', 1 ); function myprefix_query_offset(&$query) { ... } but there have been no changes. How to set up for my function? [link]codepen.io/alexfree/pen/NxPPbx[/link] – harley Dec 7 '15 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.