【问题标题】:Add pagination in wordpress admin in my own customized plugin [closed]在我自己的自定义插件中的 wordpress 管理中添加分页 [关闭]
【发布时间】:2011-07-16 09:19:24
【问题描述】:

我想在我创建的插件页面中显示分页.. 我尝试了很多示例,但没有人在工作..

如果有人能给出答案,我将不胜感激......

注意:我希望在后端(在管理员中)而不是在前端进行分页

【问题讨论】:

标签: php wordpress


【解决方案1】:

我以不同的方式解决了这个问题。解决方案是here

我使用第二个选项,标题为“如何将分页添加到 WordPress 插件?”。 这提供了与 WordPress 几乎相同的分页功能。

不要忘记下载那个“pagination.class.php”文件,把它放在你的插件文件夹中,并在你的插件中包含这个文件时给出适当的路径。

【讨论】:

【解决方案2】:

通用分页功能。

这个例子在大型数据库上看起来像这样:

上一个 1 2 3 4 kbd> 5 6 7 8 9 ... 822 823 下一个 >>

上一个 1 2 ... 815 816 817 818 819 820 821 822 823 下一个 >>

上一个 1 2 ... 812 813 814 815 816 817 818 ... 822 823 下一个 >>

享受...

<?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;
}
?>

【讨论】:

    【解决方案3】:

    简单的步骤:

    $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' => __( '&laquo;', 'text-domain' ),
        'next_text' => __( '&raquo;', '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>';
    }
    

    【讨论】:

    • 不要直接获取$_GET var 中的内容,因为这是一个安全漏洞。使用filter_input 方法,如下所示:$pageNum = filter_input(INPUT_GET, 'pageNum') ? absint(filter_input(INPUT_GET, 'pageNum')) : 1;
    • @Stan 他没有那样做,实际上absint($v) 是一个“Synonym of abs( intval( $foo ) )”——在它上面使用 intval() 之后是安全的...... 旁注: 更快的是absint((int)$_GET['pagenum'])
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    相关资源
    最近更新 更多