programing

사용자 정의된 플러그인의 wordpress admin에 페이지 번호 추가

showcode 2023. 3. 6. 21:37
반응형

사용자 정의된 플러그인의 wordpress admin에 페이지 번호 추가

제가 작성한 플러그인 페이지 중 하나에 페이지 번호를 표시하고 싶습니다.많은 예를 들어봤지만 아무도 일하지 않는다.

대답해주면 고맙겠다...

주의: 프론트 엔드가 아닌 백 엔드(admin의 경우)에서 페이지 매김을 원합니다.

간단한 절차:

$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;

총 레코드 수 검색

$limit = 10; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "SELECT COUNT(`id`) FROM {$wpdb->prefix}table_name" );
$num_of_pages = ceil( $total / $limit );

제한 부여:

$entries = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}table_name LIMIT $offset, $limit" );

페이지 번호 지정이 필요한 곳에 이 코드를 추가합니다.

$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '«', 'text-domain' ),
    'next_text' => __( '»', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}

일반 페이지 번호 매기기 기능.

다음 예는 대규모 DB에서 다음과 같습니다.

<< Previous 1 2 3 4 5 6 7 8 9 ...822 823 Next >>

또는

<< Previous 1 2 ... 815 816 817 818 819 820 821 822 823 Next >>

또는

<< Previous 1 2 ...813814815816817818822 823 Next >>

즐기다

<?php 
/*Max Number of results to show*/
$max = 10;
/*Get the current page eg index.php?pg=4*/

if(isset($_GET['pg'])){
    $p = (int) $_GET['pg'];
}else{
    $p = 1;
}

$limit = ($p - 1) * $max;
$prev = $p - 1;
$next = $p + 1;
$limits = (int)($p - 1) * $max;

//This is the query to get the current dataset (change api to suit)
$result = mysqli_query($res, 'SELECT * from yourtable limit '.$limits.','.$max.'');

//Get total records from db (change api to suit)
$totalres = mysqli_result($res, mysqli_query($res, 'SELECT COUNT(id) AS tot FROM yourtable"'),0);

//devide it with the max value & round it up
$totalposts = ceil($totalres / $max);
$lpm1 = $totalposts - 1;

echo pagination($totalposts, $p, $lpm1, $prev, $next);

//The function
function pagination($totalposts, $p, $lpm1, $prev, $next){
    $adjacents = 3;
    if($totalposts > 1)
    {
        $pagination .= "<center><div>";
        //previous button
        if ($p > 1)
        $pagination.= "<a href=\"?pg=$prev\"><< Previous</a> ";
        else
        $pagination.= "<span class=\"disabled\"><< Previous</span> ";
        if ($totalposts < 7 + ($adjacents * 2)){
            for ($counter = 1; $counter <= $totalposts; $counter++){
                if ($counter == $p)
                $pagination.= "<span class=\"current\">$counter</span>";
                else
                $pagination.= " <a href=\"?pg=$counter\">$counter</a> ";}
        }elseif($totalposts > 5 + ($adjacents * 2)){
            if($p < 1 + ($adjacents * 2)){
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
                    if ($counter == $p)
                    $pagination.= " <span class=\"current\">$counter</span> ";
                    else
                    $pagination.= " <a href=\"?pg=$counter\">$counter</a> ";
                }
                $pagination.= " ... ";
                $pagination.= " <a href=\"?pg=$lpm1\">$lpm1</a> ";
                $pagination.= " <a href=\"?pg=$totalposts\">$totalposts</a> ";
            }
            //in middle; hide some front and some back
            elseif($totalposts - ($adjacents * 2) > $p && $p > ($adjacents * 2)){
                $pagination.= " <a href=\"?pg=1\">1</a> ";
                $pagination.= " <a href=\"?pg=2\">2</a> ";
                $pagination.= " ... ";
                for ($counter = $p - $adjacents; $counter <= $p + $adjacents; $counter++){
                    if ($counter == $p)
                    $pagination.= " <span class=\"current\">$counter</span> ";
                    else
                    $pagination.= " <a href=\"?pg=$counter\">$counter</a> ";
                }
                $pagination.= " ... ";
                $pagination.= " <a href=\"?pg=$lpm1\">$lpm1</a> ";
                $pagination.= " <a href=\"?pg=$totalposts\">$totalposts</a> ";
            }else{
                $pagination.= " <a href=\"?pg=1\">1</a> ";
                $pagination.= " <a href=\"?pg=2\">2</a> ";
                $pagination.= " ... ";
                for ($counter = $totalposts - (2 + ($adjacents * 2)); $counter <= $totalposts; $counter++){
                    if ($counter == $p)
                    $pagination.= " <span class=\"current\">$counter</span> ";
                    else
                    $pagination.= " <a href=\"?pg=$counter\">$counter</a> ";
                }
            }
        }
        if ($p < $counter - 1)
        $pagination.= " <a href=\"?pg=$next\">Next >></a>";
        else
        $pagination.= " <span class=\"disabled\">Next >></span>";
        $pagination.= "</center>\n";
    }
    return $pagination;
}
?>

나는 이 문제를 다른 방법으로 해결했다.해답은 여기 있습니다.

"How To Add Pagination To WordPress Plugin?"이라는 제목의 두 번째 옵션을 사용합니다.이는 WordPress와 거의 동일한 페이지 표시 기능을 제공합니다.

잊지 말고 "pagination.class.php" 파일을 다운로드하여 플러그인 폴더에 넣고 이 파일을 플러그인에 포함시키면서 적절한 경로를 지정하십시오.

언급URL : https://stackoverflow.com/questions/5322266/add-pagination-in-wordpress-admin-in-my-own-customized-plugin

반응형