如何进行管理员清理和分页

时间:2011-04-04 作者:JM at Work

我想知道如何创建一个管理页面来执行CRUD。我知道如何在管理菜单中添加基本/选项页面。但我想更进一步,如何管理数据(CRUD),对于索引/列表页面,我需要分页,有人能给我指出正确的方向吗?

2 个回复
SO网友:MikeSchinkel

在WordPress中对99%的用例执行CRUD的方法是创建一个Custom Post Type. CPT将几乎免费为您提供CRUD,然后您可能需要在这里询问更多关于如何执行所选其他操作的问题,具体取决于您的特定用例。祝你好运

SO网友:JM at Work

最后我编写了一些类。

学分:http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin/

第二个想法很长,但有效。。。如果有人改进它,最好在评论中添加一个链接:)

class Pager {
    protected $table;
    protected $pages;
    protected $perpage;
    protected $page;
    protected $wpdb;
    protected $otherSQL;

    public function __construct($table, $perpage, $page, $otherSQL = \'\') {
        global $wpdb;
        $this->wpdb = $wpdb;
        $this->table = $wpdb->prefix . $table;
        $this->perpage = $perpage;
        $this->page = $page;
        $this->pages = ceil($wpdb->get_var(\'SELECT COUNT(*) FROM \' . $this->table) / $this->perpage);
        $this->otherSQL = $otherSQL;
    }

    protected function getFirstPageURL() {
        return UrlUtils::appendQueryString(\'pagenum\', 1);
    }

    protected function getPrevPageURL() {
        return UrlUtils::appendQueryString(\'pagenum\', $this->page == 1 ? 1 : $this->pagenum - 1);
    }

    protected function getNextPageURL() {
        return UrlUtils::appendQueryString(\'pagenum\', $this->page == $this->pages ? $this->pages : $this->page + 1);
    }

    protected function getLastPageURL() {
        return UrlUtils::appendQueryString(\'pagenum\', $this->pages);
    }

    protected function getPageURL($page) {
        return UrlUtils::appendQueryString(\'pagenum\', $page);
    }

    public function generatePagination($range = 2) {
        $shown = $range * 2 + 1;
        ?>
        <div class="tablenav">
            <div class="tablenav-pages">
                <span class="displaying-num"><?php echo $this->pages; ?> page(s)</span>
                <a href="<?php echo $this->getFirstPageURL(); ?>" <?php if ($this->page == 1) echo \'class="disabled"\';  ?>>&laquo;</a>
                <a href="<?php echo $this->getPrevPageURL(); ?>" <?php if ($this->page == 1) echo \'class="disabled"\';  ?>>&lsaquo;</a> 

                <?php
                for ($i = 1; $i <= $this->pages; $i++) { 
                    if ($this->pages != 1 &&( !($i >= $this->pages + $shown || $i <= $this->page - $shown) || $this->pages <= $shown )) {
                        echo \'<a href="\' . $this->getPageURL($i) . \'" \' . ($i == $this->page ? \'class="disabled"\' : \'\' ) . \'>\' . $i . \'</a>\';
                    }
                }
                ?>

                <a href="<?php echo $this->getNextPageURL(); ?>" <?php if ($this->page == $this->pages) echo \'class="disabled"\';  ?>>&raquo;</a>
                <a href="<?php echo $this->getLastPageURL(); ?>" <?php if ($this->page == $this->pages) echo \'class="disabled"\';  ?>>&rsaquo;</a>
            </div>
        </div>
        <?php
    }

    public function getPageData() {
        $data = $this->wpdb->get_results(\'SELECT * FROM \' . $this->table . \' \' . $this->otherSQL . \' LIMIT \' . ($this->page - 1) * $this->perpage . \', \' . $this->perpage, OBJECT );
        return $data;
    }

}

class UrlUtils {

    public static function parseQueryString($url = \'\') {
        $queryArr = array();
        $queryStr = $_SERVER[\'QUERY_STRING\'];
        if (!empty($url)) {
            $queryStr = parse_url($url, PHP_URL_QUERY);
        }
        parse_str($queryStr, $queryArr);
        return $queryArr;
    }

    public static function appendQueryString($key, $val, $url = "") {
        $queryArr = self::parseQueryString($url);
        $queryArr[$key] = $val;
        $queryStr = http_build_query($queryArr);

        $url = empty($url) ? $_SERVER[\'PHP_SELF\'] : $url;
        $parts = parse_url($url);
        $parts[\'query\'] = $queryStr;

        return $parts[\'path\'] . \'?\' . $parts[\'query\'];
    }

    public static function removeQueryString($remove) {
        $queryArr = self::parseQueryString();
        if (is_array($remove)) {
            foreach ($remove as  $r) {
                if (array_key_exists($r, $queryArr)) {
                    unset($queryArr[$r]);
                }
            }
        } else {
            if (array_key_exists($remove, $queryArr)) {
                unset($queryArr[$remove]);
            }
        }

        $queryStr = http_build_query($queryArr);
        return $_SERVER[\'PHP_SELF\'] . \'?\' . $queryStr;
    }

}

// Usage like

require_once(\'inc/pager.php\');
$pager = new Pager(\'km_contact\', 20, (isset($_GET[\'pagenum\']) && is_numeric($_GET[\'pagenum\'])) ? $_GET[\'pagenum\'] : 1, \'ORDER BY dt DESC\');
$contacts = $pager->getPageData();

...

<?php foreach ($contacts as $c) : ?>
    <tr>
        <td><?php echo $c->name; ?></td>
        <td><a href="mailto:<?php echo $c->name; ?>"><?php echo $c->email; ?></a></td>
        <td><?php echo $c->subject; ?></td>
        <td><?php echo $c->content; ?></td>
        <td><a href="<?php echo UrlUtils::appendQueryString(\'viewid\', $c->id); ?>">View</a></td>
    </tr>
<?php endforeach; ?>
...

$pager->generatePagination();

结束

相关推荐

Pagination with custom loop

我的问题可能是Pagination not working with custom loop, 但有一种不同。我使用自定义循环来显示flash游戏。我想按类别在游戏页面上分页。类别php:<?php if ($cat) { $cols = 2; $rows = 4; $paged = ((\'paged\')) ? get_query_var(\'paged\') : 1; $post_per_page = $cols * $rows; // -1 s