使用 $wpdb->get_results 的 WordPress 自定义分页 [英] WordPress custom pagination with $wpdb->get_results

查看:21
本文介绍了使用 $wpdb->get_results 的 WordPress 自定义分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过 $wpdb->get_results 从 Wordpress 数据库中获取自定义数据 $results = $wpdb->get_results( $query, OBJECT ); 但是我想分页使用 paginate_links();

I was able to get custom data from Wordpress database by $wpdb->get_results like so $results = $wpdb->get_results( $query, OBJECT ); However i would like to paginate the data using paginate_links();

目前没有显示带有分页链接的数据,我认为我的错误可能在 $results = $wpdb->get_results( $query.'ORDER BY id DESC LIMIT'. $offset.', '. $items_per_page, OBJECT );

Which is presently displaying no data with pagination links, I think my error is probably within $results = $wpdb->get_results( $query.'ORDER BY id DESC LIMIT'. $offset.', '. $items_per_page, OBJECT );

我的代码:

    global $wpdb;
    $table_name = $wpdb->prefix . 'templates';
    $items_per_page = 3;
    $offset = ( $page * $items_per_page ) - $items_per_page;

    $page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;

    $query = 'SELECT * FROM '.$table_name;

    $total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table";

    $total = $wpdb->get_var( $total_query );


    $results = $wpdb->get_results( $query.'ORDER BY id DESC LIMIT'. $offset.', '. $items_per_page, OBJECT );

    $results = $wpdb->get_results( $query, OBJECT );

    if(!empty($results)) {
    echo"<table class=\"table table-hover\">";
        echo"<thead>";
            echo"<tr>";
                echo"<th>Id</th>";  
                echo"<th>Date</th>";
                echo"<th>Name</th>";
                echo"<th>Image src</th>";
                echo"<th>Category</th>";
                echo"<th>Preview Link</th>";
                echo"<th>BuiltWith</th>";
                echo"<th>Price</th>";

            echo"</tr>";
        echo"</thead>";
        echo"<tbody>";
        foreach($results as $row){  


            echo"<tr>";
                echo"<td>". $row->id . "</td>";
                echo"<td>". $row->tdateTime ."</td>";
                echo"<td>". $row->tName ."</td>";
                echo"<td>". $row->tName ."</td>";
                echo"<td>". $row->tCategory ."</td>";
                echo"<td>". $row->tPreview ."</td>";
                echo"<td>". $row->tBuiltWith . "</td>";
                echo"<td>". $row->tPrice ."</td>";
            echo"</tr>";
        }

        echo"</tbody>";
    echo"</table>";

    }


    echo paginate_links( array(

        'base' => add_query_arg( 'cpage', '%#%' ),

        'format' => '',

        'prev_text' => __('&laquo;'),

        'next_text' => __('&raquo;'),

        'total' => ceil($total / $items_per_page),

        'current' => $page

    ));

推荐答案

更新

我已经对此进行了测试,并且可以在我的网站上使用.几点:

I've tested this and it works on my site. A few things:

  • 用你的替换我的 $query

  • Replace my $query with yours

global $wpdb(根据您对全局变量的评论),因为它超出了范围!

global $wpdb (per your comment regarding global variables) since it's out of scope!

get_results() 在没有被告知的情况下返回一个对象(第二个参数是返回类型)

get_results() returns an object when not told otherwise (second parameter is the return type)

代码如下:

<?php
global $wpdb;
    $table_name = $wpdb->prefix . 'templates';
    $query = "(SELECT * FROM '.$table_name)";

    $total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table";
    $total = $wpdb->get_var( $total_query );

    $items_per_page = 3;
    $page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
    $offset = ( $page * $items_per_page ) - $items_per_page;

    $results = $wpdb->get_results( $query . " ORDER BY post_date LIMIT ${offset}, ${items_per_page}" );

  if(!empty($results)) {
    echo"<table class=\"table table-hover\">";
        echo"<thead>";
            echo"<tr>";
                echo"<th>Id</th>";
                echo"<th>Date</th>";
                echo"<th>Name</th>";
                echo"<th>Image src</th>";
                echo"<th>Category</th>";
                echo"<th>Preview Link</th>";
                echo"<th>BuiltWith</th>";
                echo"<th>Price</th>";

            echo"</tr>";
        echo"</thead>";
        echo"<tbody>";
        foreach($results as $row){


            echo"<tr>";
                echo"<td>". $row->id . "</td>";
                echo"<td>". $row->tdateTime ."</td>";
                echo"<td>". $row->tName ."</td>";
                echo"<td>". $row->tName ."</td>";
                echo"<td>". $row->tCategory ."</td>";
                echo"<td>". $row->tPreview ."</td>";
                echo"<td>". $row->tBuiltWith . "</td>";
                echo"<td>". $row->tPrice ."</td>";
            echo"</tr>";
        }

        echo"</tbody>";
    echo"</table>";

    }
    
    echo paginate_links( array(
        'base' => add_query_arg( 'cpage', '%#%' ),
        'format' => '',
        'prev_text' => __('&laquo;'),
        'next_text' => __('&raquo;'),
        'total' => ceil($total / $items_per_page),
        'current' => $page
    ));
    ?>

这篇关于使用 $wpdb->get_results 的 WordPress 自定义分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆