admin管理员组

文章数量:1123564

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

    remove_shortcode("gallery");
add_shortcode("gallery","my_gallery");

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>
                </div>
            </a> 
        </div>";

    }

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

            if($paged==$i){
                 $usl='class="selected"';
            }
            else{
                 $usl='';
            }

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

    $html .= "</div>";                              


    return $html;

}

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

    remove_shortcode("gallery");
add_shortcode("gallery","my_gallery");

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>
                </div>
            </a> 
        </div>";

    }

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

            if($paged==$i){
                 $usl='class="selected"';
            }
            else{
                 $usl='';
            }

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

    $html .= "</div>";                              


    return $html;

}
Share Improve this question edited Dec 6, 2015 at 13:18 jas 1,51511 silver badges23 bronze badges asked Dec 6, 2015 at 12:59 harleyharley 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: phpoffset and maxnumpages in pagination gallery