【问题标题】:Wordpress $wpdb pagination in plugin插件中的 Wordpress $wpdb 分页
【发布时间】:2016-01-08 07:29:36
【问题描述】:

我在为 WP 编写插件时遇到了一些问题。我需要在我的数据库中显示一些内容信息。我编写了一个代码来显示来自我的数据库的查询,因为我遇到了分页问题,​​我为它写了很多帖子(例如:Paginate Wordpress $wpdb),但我只是不明白我需要对我的代码进行分页做什么。这是我的代码的一部分,它显示了我的数据库中的一些数据: $sSql = "SELECT * FROM pam INNER JOIN images ON pam.id_fake=images.id_zapisWHERE id_type = $type LIMIT ... OFFSET ...";

$myData = array();
$myData = $wpdb->get_results($sSql, ARRAY_A);

$count = $wpdb->num_rows;

echo '<div class="tableCont"> <table>';

for ($i = 0; $i<$count; $i++) {
    echo '<tr>
            <td>';
            if($myData[$i+1]['id_fake']==$myData[$i]['id_fake']){

            echo '<a class="gallery" rel="group" title="'.$myData[$i]['description'].'" href="'.get_option('siteurl').'/wp-content/uploads/'.$myData[$i]['image'].'">
                  <img src="'.get_option('siteurl').'/wp-content/uploads/'.imgsmall($myData[$i]['image']).'" class="images" alt=""></a>';
                while($myData[$i+1]['id_fake']==$myData[$i]['id_fake']){
                    echo '<a class="gallery" rel="group" title="'.$myData[$i]['description'].'" href="'.get_option('siteurl').'/wp-content/uploads/'.$myData[$i]['image'].'">
                          <img src="'.get_option('siteurl').'/wp-content/uploads/'.imgsmall($myData[$i]['image']).'" class="images" alt=""></a>';
                    $i++;
                }
            }
            else{
                echo '<a class="gallery" title="'.$myData[$i]['description'].'" href="'.get_option('siteurl').'/wp-content/uploads/'.$myData[$i]['image'].'">
                  <img src="'.get_option('siteurl').'/wp-content/uploads/'.imgsmall($myData[$i]['image']).'" class="images" alt="" ></a>';
            }
    echo   '</td>
            <td class="description">'.$myData[$i]['description'].'</td>
            <td class="price">Price:<br><br>'.$myData[$i]['price'].'</td>
            </tr>';
}
echo '</table></div>';

你能帮我分页这段代码吗?

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    流程的起点是查询,首选的解决方案是使用一个常量来处理它(注意使用$wpdb->prepare to sanitize the query parameters):

    $count = 10;
    $from = (isset($_GET['current_page'])) ? $_GET['current_page'] : 0
    $sSql = "SELECT SQL_CALC_FOUND_ROWS * FROM pam INNER JOIN images ON pam.id_fake=images.id_zapis WHERE id_type = %d LIMIT %d,%d";
    $myData = $wpdb->get_results($wpdb->prepare($sSql, array($type,$from,$count)), ARRAY_A);
    $total = $wpdb->get_var("SELECT FOUND_ROWS()");
    
    <YOUR LOOP HERE TO DISPLAY THE RESULTS>
    
    /* To include the pagination links */ 
    echo paginate_links(array(
        'base'         => '%_%',
        'format'       => '?current_page=%#%',
        'total'        => ceil($total/$count),
        'current'      => max($from,1)
        ));
    

    【讨论】:

      猜你喜欢
      • 2011-06-16
      • 1970-01-01
      • 2011-09-07
      • 2012-01-23
      • 1970-01-01
      • 2015-11-13
      • 2015-09-18
      • 2016-02-13
      • 2013-07-05
      相关资源
      最近更新 更多