在 WordPress 中对 get_posts() 进行分页

发布于 2024-09-12 03:49:42 字数 595 浏览 2 评论 0原文

这是我的 WordPress 中的模板页面(已删除 html),它从我的 WordPress 数据库中提取帖子。我想分页,但不知道! :(

我不想得到这样的东西

<<首页 上一页 1 2 3 4 下一页 最后一页 >>

       <?php
         $postslist = get_posts('numberposts=10&order=ASC');
         foreach ($postslist as $post) : 
            setup_postdata($post);
    ?>

      <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?>   </a></li>
     <li><?php the_excerpt(); ?></li><br/><hr/>

     <?php endforeach; ?>

This a template page in my wordpress (have removed html), it pulls out the posts from my wordpress database. I want to paginate it but have no idea! :(

I wan't to get something like this

<< First Prev 1 2 3 4 Next Last >>

       <?php
         $postslist = get_posts('numberposts=10&order=ASC');
         foreach ($postslist as $post) : 
            setup_postdata($post);
    ?>

      <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?>   </a></li>
     <li><?php the_excerpt(); ?></li><br/><hr/>

     <?php endforeach; ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2024-09-19 03:49:44

这是我使用的分页类:

<?php

class sitePagination{

    public $currentPage;
    public $perPage;
    public $totalRecords;

    public function __construct($page = 1,$per_page = 2, $total_records = 0){
        $this->currentPage = (int)$page;
        $this->perPage = (int)$per_page;
        $this->totalRecords = (int)$total_records;  
    }

    public function totalPages(){
        return ceil($this->totalRecords / $this->perPage);
    }

    public function previousPage(){
        return $this->currentPage - 1;
    }

    public function nextPage(){
        return $this->currentPage + 1;
    }

    public function previousPageExists(){
        return $this->previousPage() >= 1 ? true : false; 
    }

    public function nextPageExists(){
        return $this->nextPage() <= $this->totalPages() ? true : false; 
    }

    public function offset(){
        return ($this->currentPage - 1) * $this->perPage;
    }

}

?>

然后在 page.html 中,我使用了这个:

//include the paginate class. I put it in the theme folder
    include("paginate.php");

    //This is the SQL Query to get the number of rows I have
    $count = "SELECT COUNT(*) FROM $wpdb->blogs WHERE site_id = $wpdb->siteid AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00'"; 

    $number =  mysql_query($count);
    $row = mysql_fetch_array($number);
    $num_rows = array_shift($row);

    //Define some variable to hold our pagination settings
    $page = !empty($_GET['current_page']) ? (int)$_GET['current_page'] : 1;
    $perPage = 5;
    $paginate  =  new sitePagination($page,$perPage,$num_rows);

    //This is the actual SQL Query to fetch the Data from Database
    $query = $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC LIMIT {$perPage} OFFSET {$paginate->offset()}", $wpdb->siteid );
    //echo "query is $query<br/>";

    $terms = $wpdb->get_results($query,ARRAY_A);
    echo "<ul>";

    foreach($terms as $detail)
    {
        //$cat_parent = get_category($detail->parent);
        //Had to use the $cat_parent to build the link
        //if some has a better idea, would be nice
        echo "<li style='font-size:1.3em;'><a href='http://".$detail[ 'domain' ].$detail['path']."'>".get_blog_option( $detail['blog_id'], 'blogname' )."</a></li>";
    }

    // The Fun starts here, all the code below will generate our dynamic page number
    // The container class is the same as WP PAGENAVI
    // I'm using a custom pagination class I created
    echo "</ul>";

    //echo "<span class='pages'>Page {$page} of {$paginate->totalPages()}</span>";
    if($paginate->totalPages() > 1){
        if($paginate->previousPageExists()){
            echo '<a href="?cat='.$cat_parent->term_id.'¤t_page='.$paginate->previousPage().'">« Previous</a>';
        }
    }
    if(ceil($paginate->totalPages()) > 1){
        for($i=1;$i < ceil($paginate->totalPages()) + 1;$i++){
            if($page == $i)
                echo '<span class="current"> '.$i.' </span>';
            else
                echo '<a href="'.$cat_parent->slug.'/?current_page='.$i.'"> '.$i.' </a>';

        }
    }

    if($paginate->totalPages() > 1){
        if($paginate->nextPageExists()){
            echo '<a href="?cat='.$cat_parent->term_id.'¤t_page='.$paginate->nextPage().'">Next »</a>';
        }

    }

Here's a pagination class I use:

<?php

class sitePagination{

    public $currentPage;
    public $perPage;
    public $totalRecords;

    public function __construct($page = 1,$per_page = 2, $total_records = 0){
        $this->currentPage = (int)$page;
        $this->perPage = (int)$per_page;
        $this->totalRecords = (int)$total_records;  
    }

    public function totalPages(){
        return ceil($this->totalRecords / $this->perPage);
    }

    public function previousPage(){
        return $this->currentPage - 1;
    }

    public function nextPage(){
        return $this->currentPage + 1;
    }

    public function previousPageExists(){
        return $this->previousPage() >= 1 ? true : false; 
    }

    public function nextPageExists(){
        return $this->nextPage() <= $this->totalPages() ? true : false; 
    }

    public function offset(){
        return ($this->currentPage - 1) * $this->perPage;
    }

}

?>

And then in the page.html, I used this:

//include the paginate class. I put it in the theme folder
    include("paginate.php");

    //This is the SQL Query to get the number of rows I have
    $count = "SELECT COUNT(*) FROM $wpdb->blogs WHERE site_id = $wpdb->siteid AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00'"; 

    $number =  mysql_query($count);
    $row = mysql_fetch_array($number);
    $num_rows = array_shift($row);

    //Define some variable to hold our pagination settings
    $page = !empty($_GET['current_page']) ? (int)$_GET['current_page'] : 1;
    $perPage = 5;
    $paginate  =  new sitePagination($page,$perPage,$num_rows);

    //This is the actual SQL Query to fetch the Data from Database
    $query = $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC LIMIT {$perPage} OFFSET {$paginate->offset()}", $wpdb->siteid );
    //echo "query is $query<br/>";

    $terms = $wpdb->get_results($query,ARRAY_A);
    echo "<ul>";

    foreach($terms as $detail)
    {
        //$cat_parent = get_category($detail->parent);
        //Had to use the $cat_parent to build the link
        //if some has a better idea, would be nice
        echo "<li style='font-size:1.3em;'><a href='http://".$detail[ 'domain' ].$detail['path']."'>".get_blog_option( $detail['blog_id'], 'blogname' )."</a></li>";
    }

    // The Fun starts here, all the code below will generate our dynamic page number
    // The container class is the same as WP PAGENAVI
    // I'm using a custom pagination class I created
    echo "</ul>";

    //echo "<span class='pages'>Page {$page} of {$paginate->totalPages()}</span>";
    if($paginate->totalPages() > 1){
        if($paginate->previousPageExists()){
            echo '<a href="?cat='.$cat_parent->term_id.'¤t_page='.$paginate->previousPage().'">« Previous</a>';
        }
    }
    if(ceil($paginate->totalPages()) > 1){
        for($i=1;$i < ceil($paginate->totalPages()) + 1;$i++){
            if($page == $i)
                echo '<span class="current"> '.$i.' </span>';
            else
                echo '<a href="'.$cat_parent->slug.'/?current_page='.$i.'"> '.$i.' </a>';

        }
    }

    if($paginate->totalPages() > 1){
        if($paginate->nextPageExists()){
            echo '<a href="?cat='.$cat_parent->term_id.'¤t_page='.$paginate->nextPage().'">Next »</a>';
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文